Java 如何为充气后的每个EditText设置ID?
我做了这些代码。 每次我点击按钮,一个编辑文本就会在滚动布局中膨胀。 我怎样才能在充气时获得每个编辑文本的ID? 去计算每一个的价值Java 如何为充气后的每个EditText设置ID?,java,android,android-inflate,Java,Android,Android Inflate,我做了这些代码。 每次我点击按钮,一个编辑文本就会在滚动布局中膨胀。 我怎样才能在充气时获得每个编辑文本的ID? 去计算每一个的价值 ScrollView scrollview; LinearLayout linearLayout; LinearLayout.LayoutParams layoutParams; //EditText hello; static int i = 0; @Override protected void onCreate(Bundle savedInstanceS
ScrollView scrollview;
LinearLayout linearLayout;
LinearLayout.LayoutParams layoutParams;
//EditText hello;
static int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sum_1 = (Button)findViewById(R.id.sum);
scrollview = (ScrollView)findViewById(R.id.scrollview);
linearLayout = (LinearLayout)findViewById(R.id.gamehistory);
Button b = (Button)findViewById(R.id.Button01);
layoutParams = new LinearLayout.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
b.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
EditText hello = new EditText(MainActivity.this);
hello.setText("enter value" + i++);
linearLayout.addView(hello, layoutParams);
hello.setId(i++);
}
});
您希望向布局添加ID的唯一原因是为了在代码中获得该视图。。在这里,您可以访问编辑文本 将所有编辑文本添加到列表中,然后从列表中使用它们,这样更容易 甚至是EditText[]
b.setOnClickListener(new View.OnClickListener()){
b.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
EditText hello = new EditText(MainActivity.this);
hello.setText("enter value" + i++);
linearLayout.addView(hello, layoutParams);
hello.setId(i++);
hello.setTag("edit");
}
});
int childcount = linearLayout.getChildCount();
for (int i = 0; i < childcount; i++) {
View v1 = linearLayout.getChildAt(i);
if(v1.getTag.equls("edit"){
System.out.println(v1..getId());
}
}
公共void onClick(视图v){
EditText hello=新的EditText(MainActivity.this);
hello.setText(“输入值”+i++);
linearLayout.addView(您好,布局参数);
你好,setId(i++);
hello.setTag(“编辑”);
}
});
int childcount=linearLayout.getChildCount();
for(int i=0;i
当您在膨胀EditText时,您可以使用setId(int currentId)分配一个ID。当您膨胀它时,这个currentId将是静态值,增加1。thanx,但是childcount是多少?大约,但它在“if(v1.getTag.equals(“编辑”)”行上显示错误消息,因为“v1”你有你的解决方案吗?非常感谢。你对我很有用。我很感激。对不起“投票需要15个声誉”