一个简单的例子:
Dialog d=new Dialog(Activity.this);
LinearLayout linearLayout=new LinearLayout(Activity.this);
TextView textview1=new TextView(Activity.this);
TextView textview2=new TextView(Activity.this);
TextView textview3=new TextView(Activity.this);
TextView textview4=new TextView(Activity.this);
......
linearLayout.addView(textview1);
linearLayout.addView(textview2);
linearLayout.addView(textview3);
linearLayout.addView(textview4);
.....
d.setContentView(linearLayout);
d.show();
我只是使用Copy& Paste并在视图名称的末尾更改1,2,3,4 ......但是如果有一个OnClickListener并且在单击后打开一个新对话框(依此类推) ),我得到了大量的课程,只有复制和粘贴代码。有更好的方法吗?
答案 0 :(得分:3)
阵列怎么样?
Dialog d=new Dialog(Activity.this);
LinearLayout linearLayout=new LinearLayout(Activity.this);
int n=5;
TextView[] text_views = new TextView[n];
for(TextView text_view : text_views)
{
text_view=new TextView(Activity.this);
linearLayout.addView(text_view);
}
d.setContentView(linearLayout);
d.show();