zoukankan      html  css  js  c++  java
  • Android动态生成表格

    1:首先编写layout的布局文件的编写。

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context="com.huanglinbin.table.MainActivity" >
    10   
    11     <LinearLayout 
    12         android:id="@+id/l1"
    13         android:layout_width="match_parent"
    14         android:layout_height="wrap_content"
    15         android:orientation="horizontal"
    16         >
    17         
    18         <TextView 
    19             android:layout_width="wrap_content"
    20             android:layout_height="wrap_content"
    21             android:text="请输入列数"
    22             />
    23         
    24         <EditText
    25             android:id="@+id/et1"
    26             android:layout_width="match_parent"
    27             android:layout_height="wrap_content"
    28             android:numeric="decimal"
    29             android:layout_marginLeft="10dp"
    30          />
    31         
    32     </LinearLayout>
    33     
    34     <LinearLayout
    35         android:id="@+id/l2" 
    36         android:layout_below="@id/l1"
    37         android:layout_width="match_parent"
    38         android:layout_height="wrap_content"
    39         android:orientation="horizontal"
    40         >
    41         
    42         <TextView 
    43             android:layout_width="wrap_content"
    44             android:layout_height="wrap_content"
    45             android:text="请输入行数"
    46             />
    47         <-- android:numeric="decimal"表示的是输入框中只能是数字,不能输入其他数值 --!>
    48         <EditText
    49             android:id="@+id/et2"
    50             android:layout_width="match_parent"
    51             android:layout_height="wrap_content"
    52             android:numeric="decimal"
    53             android:layout_marginLeft="10dp"
    54          />
    55         
    56     </LinearLayout>
    57     
    58     <LinearLayout 
    59         android:id="@+id/l3"
    60         android:layout_below="@id/l2"
    61         android:layout_width="match_parent"
    62         android:layout_height="wrap_content"
    63         android:orientation="horizontal"
    64         >
    65     
    66         <Button 
    67             android:id="@+id/bt"
    68             android:layout_width="wrap_content"
    69             android:layout_height="wrap_content"
    70             android:text="按钮"
    71             />
    72         
    73     </LinearLayout>
    74     
    75     <LinearLayout 
    76         android:layout_below="@id/l3"
    77         android:layout_width="match_parent"
    78         android:layout_height="wrap_content"
    79         android:orientation="horizontal"
    80         
    81         >
    82         
    83         <TableLayout 
    84             android:id="@+id/table"
    85             android:layout_width="match_parent"
    86             android:layout_height="wrap_content"
    87             
    88             >
    89           
    93         </TableLayout>
    96     </LinearLayout>
    97    
    99 </RelativeLayout>

    2:编写源文件的代码。

     1 import android.app.Activity;
     2 import android.os.Bundle;
     3 import android.view.View;
     4 import android.view.View.OnClickListener;
     5 import android.view.ViewGroup;
     6 import android.widget.Button;
     7 import android.widget.EditText;
     8 import android.widget.TableLayout;
     9 import android.widget.TableRow;
    10 import android.widget.TextView;
    11 import android.widget.Toast;
    12 
    13 public class MainActivity extends Activity {
    14     
    15     //ViewGroup.LayoutParams.WRAP_CONTENT相当于layout文件中的wrap_content的属性的值。
    16     private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;    
    17     //ViewGroup.LayoutParams.WRAP_CONTENT相当于layout文件中的match_parent属性的值。
    18     private final int FP = ViewGroup.LayoutParams.MATCH_PARENT;
    19 
    20     
    21     protected void onCreate(Bundle savedInstanceState) {
    22         super.onCreate(savedInstanceState);
    23         setContentView(R.layout.activity_main);
    24         //先要找到按钮。
    25         Button bt = (Button) findViewById(R.id.bt);
    26         //首先要先获取到列数的文本框。
    27         final EditText et1 = (EditText) findViewById(R.id.et1);
    28         //首先要先获取到行数的文本框。
    29         final EditText et2 = (EditText) findViewById(R.id.et2);
    30         bt.setOnClickListener(new OnClickListener() {
    31             public void onClick(View v) {
    32                 if(et1.getText().length()>0&&et2.getText().length()>0){
    33                     //拿到文本框之后就获取到里面的值。
    34                     int et1_int = Integer.parseInt(et1.getText().toString());
    35                     int et2_int = Integer.parseInt(et2.getText().toString());
    36                     //新建一个tableLayout的实例。
    37                     TableLayout table = (TableLayout) findViewById(R.id.table);
    38                     //每点击一次就先删除表格。
    39                     table.removeAllViews();
    40                     //全部列自动填充空白处。
    41                     table.setStretchAllColumns(true);
    42                     //循环生成行数
    43                     for(int i=0;i<et2_int;i++){
    44                         //创建一个tableRow
    45                         TableRow tablerow = new TableRow(MainActivity.this);
    46                         //循环生成列数。
    47                         for(int j=0;j<et1_int;j++){
    48                             //创建一个用来显示的Textview.
    49                             TextView textview = new TextView(MainActivity.this);
    50                             //把数值设置进去。
    51                             textview.setText("("+i+","+j+")");
    52                             tablerow.addView(textview);
    53                         }
    54                          //新建的TableRow添加到TableLayout     
    55                         table.addView(tablerow, new TableLayout.LayoutParams(FP, WC,1));    
    56                     }
    57                 }else{
    58                     //没有输入内容的时候就会跳出一个提示框来进行提示。
    59                     Toast.makeText(MainActivity.this, "请输入行数和列数",1).show();
    60                 }
    61             }
    62         });  
    63     }
    64 }

    3:之后用模拟器和手机进行运行的时候可以动态生成表格的数据。

  • 相关阅读:
    201521123081《Java程序设计》 第8周学习总结
    201521123081《java程序设计》 第7周学习总结
    201521123076《Java程序设计》第1周学习总结
    201521123070 《JAVA程序设计》第7周学习总结
    201521123070 《JAVA程序设计》第6周学习总结
    201521123070 《JAVA程序设计》第5周学习总结
    201521123070 《JAVA程序设计》第4周学习总结
    201521123070 《JAVA程序设计》第3周学习总结
    201521123070 《JAVA程序设计》第2周学习总结
    201521123070 《JAVA程序设计》第1周学习总结
  • 原文地址:https://www.cnblogs.com/huanglinbin/p/6093736.html
Copyright © 2011-2022 走看看