zoukankan      html  css  js  c++  java
  • 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)中.

  • 相关阅读:
    Linux 删除多余IP地址
    linux 变更网卡后无法联网
    eureka 参数
    C# 一般处理程序使用session注意事项
    asp.net web 简单使用cookie
    asp.net ajax post 请求
    Ajax 的基本使用以及get请求
    asp.net 错误页
    C# winfrom 跨线程访问文本框
    C# winfrom 打印到Excel中
  • 原文地址:https://www.cnblogs.com/wdht/p/6095225.html
Copyright © 2011-2022 走看看