android中如何动态创建数据表

android中如何动态创建数据表,第1张

在布局中加入表格

<TableLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/table1">

</TableLayout>

之后再 MainActivity 中写入动态添加的代码

public void click(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=1i<=row_inti++)

{

TableRow tableRow=new TableRow(MainActivity.this)

for(int j=1j<=col_intj++)

{

//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()

}

}

好像有个方法,用listview 在他item中可以动态添加各种控件的,具体看下安卓listview的用法,我也遇到过,不过我是在listview 里面添加8行数据有textview 和EditText


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11400087.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-15
下一篇2023-05-15

发表评论

登录后才能评论

评论列表(0条)

    保存