Android開發之動態添加控件


動態添加TextView控件:

一:創建一個Android project項目

        activity_main.xml文件:

          1、用兩個LinearLayout布局分別包裹一對TextView,EditText控件,將orientation設置為水平方向,EditText的hint屬性可以實現水印效果,兩個EditText用來控制顯示(TextView控件數量)的行和列。

          2、 用一個LinearLayout布局包裹Button按鈕,在EditText控件輸入完后,點擊button按鈕,就會自動生成控件。

          3、 用一個TableLayout布局用表格的形式顯示生成的控件。

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
                
            <TextView 
                android:layout_width="wrap_content"
                android:gravity="center_vertical"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:textSize="20sp"
                android:text="行:"
                />

            <EditText
                android:id="@+id/editText1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="請輸入數字!"
                android:numeric="decimal" />
            
        </LinearLayout>
        
        <LinearLayout
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            
            <TextView 
                android:layout_width="wrap_content"
                android:gravity="center_vertical"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:textSize="20sp"
                android:text="列:"
                />

            <EditText
                android:id="@+id/editText2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="請輸入數字!"
                android:numeric="decimal">

                <requestFocus />
            </EditText>
                
        </LinearLayout>
        
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" 
            >
            
            <Button 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/button1"
                android:text="生成表格"
                />
            
        </LinearLayout>
        
    </LinearLayout>


    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:id="@+id/table1">
        
    </TableLayout>

   MainActivity.java文件:

         1、WC和MP變量分別用來設置自動生成TextView控件的寬高。

         2、首先創建兩個EditText,一個Button,一TableLayout變量,並且通過FindViewById(R)獲取控件相對應的id.

         3、Button通過setOnClickListener(New OnClickListener){});方法添加監聽事件,實現Onclick()點擊按鈕觸發事件。

         4、兩個輸入寬通過Integer.parseInt(row.getText().toString())的方法獲取輸入的內容並將其轉換為整型,getText()獲取輸入值。

         5、代碼創建控件:通過new 控件(MainActivity.this)的方式創建控件,MainActity是匿名類。

public class MainActivity extends Activity {
    private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;    
    private final int MP = ViewGroup.LayoutParams.MATCH_PARENT;  
    private EditText row;
    private EditText column;
    private Button bt1;
    private TableLayout tableLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //獲取控件Button
        bt1=(Button) findViewById(R.id.button1);
        //獲取文本輸入框控件
        row=(EditText) findViewById(R.id.editText1);
        column=(EditText) findViewById(R.id.editText2);
        
        //給button按鈕綁定單擊事件
        bt1.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                
                if(row.getText().length()>0&&column.getText().length()>0){
                    //把輸入的行和列轉為整形
                    int row_int=Integer.parseInt(row.getText().toString());
                    int col_int=Integer.parseInt(column.getText().toString());
                    
                    
                     //獲取控件tableLayout     
                    tableLayout = (TableLayout)findViewById(R.id.table1); 
                    //清除表格所有行
                    tableLayout.removeAllViews();
                    //全部列自動填充空白處     
                    tableLayout.setStretchAllColumns(true);    
                    //生成X行,Y列的表格     
                    for(int i=1;i<=row_int;i++)    
                    {    
                        TableRow tableRow=new TableRow(MainActivity.this);    
                        for(int j=1;j<=col_int;j++)    
                        {    
                            //tv用於顯示     
                            TextView tv=new TextView(MainActivity.this);
                            //Button bt=new Button(MainActivity.this);
                            tv.setText("("+i+","+j+")");   
                           
                            tableRow.addView(tv);    
                        }    
                        //新建的TableRow添加到TableLayout
                 
                        tableLayout.addView(tableRow, new TableLayout.LayoutParams(MP, WC,1)); 
                        
                    }    
                }else{
                    Toast.makeText(MainActivity.this,"請輸入行和列",1).show();
                }
            }
        });
        
        
    }  

}

         6、雙重循環,最外面一層用來創建TableRow行數的,里面用來創建列數的。

         7、TableLayout表格布局可以通過removeAllViews()方法清除表格數據,防止點擊兩次出現重復的內容。通過setStretchAllColumns(true)設置全部列自動填充空白處。

         8、 TableLayout表格布局中的TableRow相當於table的tr標簽,可以通過addView()將tr追加到表中,控件也可以通過這個方法追加到行(TableRow)中.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM