我不知道如何在以编程方式创建的线性布局中获取商品的ID。我想“捕获”按钮的正确ID,并将其与字段相关联。
LinearLayout layout = (LinearLayout) findViewById(R.id.content_doodle_linearlayout3);
layout.setOrientation(LinearLayout.VERTICAL);
String[] data = {"1","2","3","4"};
String[] users = {"1","2","3"};
for (int i = 0; i < users.length; i++) {
row = new LinearLayout(this);
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
text = new TextView(this);
text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
text.setText("Name " + (i));
text.setId(1000 + i);
row.addView(text);
int ergebnis = -1;
for (int j = 0; j < daten.length; j++) {
CheckBox btnTag = new CheckBox(this);
btnTag.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
//btnTag.setText("Button " + (j + 1 + (i * 4)));
btnTag.setId(j + 1 + (i * 4));
row.addView(btnTag);
}
layout.addView(row);
}
找不到按钮,因为“没有”(R.id. {XML-Field})。 我如何从特定行中“找到”单击的按钮。我必须对每个按钮进行编码吗?
答案 0 :(得分:1)
您可以通过以下方式为布局中的每个子视图设置指定的标签:
View childView = ...;
childView.setTag("Some tag");
然后在您的onClickListener中:
child.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Object tag = view.getTag();
if (tag instanceof String) {
String stringTag = (String) tag;
if (stringTag.equals("Some tag") {
// do something
}
}
}
});