Android动态生成表格

【Android动态生成表格】智慧并不产生于学历,而是来自对于知识的终生不懈的追求。这篇文章主要讲述Android动态生成表格相关的知识,希望能为你提供帮助。
最近刚刚学习完android的五大布局,现在我们进一步深入学习,尝试做一个动态生成表格功能的例子
样式布局代码如下:

1 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2xmlns:tools="http://schemas.android.com/tools" 3android:layout_width="match_parent" 4android:layout_height="match_parent" 5android:orientation="vertical" > 6 7 8< LinearLayout 9android:layout_width="match_parent" 10android:layout_height="wrap_content" 11android:orientation="vertical" > 12 13< LinearLayout 14android:layout_weight="1" 15android:layout_width="match_parent" 16android:layout_height="wrap_content" 17android:orientation="horizontal" > 18 19< TextView 20android:layout_width="wrap_content" 21android:gravity="center_vertical" 22android:layout_height="match_parent" 23android:layout_marginLeft="10dp" 24android:textSize="20sp" 25android:text="请输入行:" 26/> 27 28< EditText 29android:id="@+id/editText1" 30android:layout_width="wrap_content" 31android:layout_height="wrap_content" 32android:ems="10" 33android:hint="请输入数字!" 34android:numeric="decimal" /> 35 36< /LinearLayout> 37 38< LinearLayout 39android:layout_weight="1" 40android:layout_width="match_parent" 41android:layout_height="wrap_content" 42android:orientation="horizontal" > 43 44< TextView 45android:layout_width="wrap_content" 46android:gravity="center_vertical" 47android:layout_height="match_parent" 48android:layout_marginLeft="10dp" 49android:textSize="20sp" 50android:text="请输入列:" 51/> 52 53< EditText 54android:id="@+id/editText2" 55android:layout_width="wrap_content" 56android:layout_height="wrap_content" 57android:ems="10" 58android:hint="请输入数字!" 59android:numeric="decimal"> 60 61< requestFocus /> 62< /EditText> 63 64< /LinearLayout> 65 66< LinearLayout 67android:layout_width="match_parent" 68android:layout_height="wrap_content" 69android:orientation="horizontal" 70> 71 72< Button 73android:layout_width="match_parent" 74android:layout_height="wrap_content" 75android:id="@+id/button1" 76android:text="一键自动生成表格" 77/> 78 79< /LinearLayout> 80 81< /LinearLayout> 82 83 84< TableLayout 85android:layout_width="match_parent" 86android:layout_height="match_parent" 87android:id="@+id/table1"> 88 89 90< /TableLayout> 91 92 < /LinearLayout>

逻辑代码如下:
1 package com.example.dynamicgenerationtable; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.View.OnClickListener; 7 import android.view.ViewGroup; 8 import android.widget.Button; 9 import android.widget.EditText; 10 import android.widget.TableLayout; 11 import android.widget.TableRow; 12 import android.widget.TextView; 13 import android.widget.Toast; 14 15 16 public class MainActivity extends Activity { 17private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; 18private final int MP = ViewGroup.LayoutParams.MATCH_PARENT; 19private EditText row; 20private EditText column; 21private Button bt1; 22private TableLayout tableLayout; 23 24@Override 25protected void onCreate(Bundle savedInstanceState) { 26super.onCreate(savedInstanceState); 27setContentView(R.layout.activity_main); 28//获取控件Button 29bt1=(Button) findViewById(R.id.button1); 30//获取文本输入框控件 31row=(EditText) findViewById(R.id.editText1); 32column=(EditText) findViewById(R.id.editText2); 33 34//给button按钮绑定单击事件 35bt1.setOnClickListener(new OnClickListener() { 36 37@Override 38public void onClick(View v) { 39 40if(row.getText().length()> 0& & column.getText().length()> 0){ 41//把输入的行和列转为整形 42int row_int=Integer.parseInt(row.getText().toString()); 43int col_int=Integer.parseInt(column.getText().toString()); 44 45 46//获取控件tableLayout 47tableLayout = (TableLayout)findViewById(R.id.table1); 48//清除表格所有行 49tableLayout.removeAllViews(); 50//全部列自动填充空白处 51tableLayout.setStretchAllColumns(true); 52//生成X行,Y列的表格 53for(int i=1; i< =row_int; i++) 54{ 55TableRow tableRow=new TableRow(MainActivity.this); 56for(int j=1; j< =col_int; j++) 57{ 58//tv用于显示 59TextView tv=new TextView(MainActivity.this); 60//Button bt=new Button(MainActivity.this); 61tv.setText("("+i+","+j+")"); 62 63tableRow.addView(tv); 64} 65//新建的TableRow添加到TableLayout 66 67tableLayout.addView(tableRow, new TableLayout.LayoutParams(MP, WC,1)); 68 69} 70}else{ 71Toast.makeText(MainActivity.this,"请输入数值",1).show(); 72} 73} 74}); 75 76 77} 78 79 }

运行效果如下图:
Android动态生成表格

文章图片

 
以上就是一个简单的动态生成表格的例子

    推荐阅读