动态添加按钮到TableRow
您好,我想单击一个按钮,每次单击该按钮时都会向表行添加一个新按钮。将 3 个按钮添加到该行后,我想动态创建一个新的表行并向其添加一个新按钮。
如果单击按钮,我知道如何将带有按钮的行添加到 tableLayout 中。我不知道如何在每次单击时修改表格行,以便添加一个附加按钮。
任何建议都会非常有帮助和感激。
以下是我的代码,但这并不完美,
public class DynamicTableView extends Activity {
TableLayout mTlayout;
String[] mTextofButton = { "Dipak", "E", "I", "J", "L",
"M", "G", "R", "N", "T", "H", "P",
"K", "Y", "V" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTlayout = (TableLayout) findViewById(R.id.mTlayout);
TableRow tr=new TableRow(this);
for(int i=0;i<mTextofButton.length;i++){
Button btn=new Button(this);
btn.setText(mTextofButton[i]);
tr.addView(btn);
}
mTlayout.addView(tr);
}
}
提前致谢。
Hello I want to click a button that will add a new button to a table row every time it is clicked. After 3 buttons have been added to the row, I want to dynamically create a new table row and add a new button to it.
I know how to add a row with a button(s) to a tableLayout if I click a button. I don't know how to modify the table row every time I click so I can add an additional button.
Any advice would be quite helpful and appreciated.
Following is my code but this is not perfect
public class DynamicTableView extends Activity {
TableLayout mTlayout;
String[] mTextofButton = { "Dipak", "E", "I", "J", "L",
"M", "G", "R", "N", "T", "H", "P",
"K", "Y", "V" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTlayout = (TableLayout) findViewById(R.id.mTlayout);
TableRow tr=new TableRow(this);
for(int i=0;i<mTextofButton.length;i++){
Button btn=new Button(this);
btn.setText(mTextofButton[i]);
tr.addView(btn);
}
mTlayout.addView(tr);
}
}
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用以下代码:-
Use following Code:-
他们会使用
Button
的onClick()
,说addNewButton()
并执行类似以下操作:但要小心您的
mTextofButton .length
,意味着它应该能被3整除,否则你会得到ArrayIndexOutOfBondException
。更好的方法是使用 ArrayListtheir would a
Button
'sonClick()
, sayaddNewButton()
and do something like this:but be careful about your
mTextofButton.length
, means it should divisable by 3 otherwise you gonna gettingArrayIndexOutOfBondException
. better method is to useArrayList<String>