【发布时间】:2014-07-29 12:14:46
【问题描述】:
我的代码如下。我只是不想为我的 EditText 和 findViewById 设置一个 ID。因为我正在构建动态用户界面。所以静态id对我来说没用。我现在正试图从 linearLayout 中获取 EditText,但它还没有工作。有什么想法吗?
LinearLayout linearLayout = new LinearLayout(this);
LayoutParams cbLayoutParams = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
locationLayout.setLayoutParams(cbLayoutParams);
locationLayout.setOrientation(LinearLayout.VERTICAL);
EditText edtText = new EditText(this);
edtText.setEnable(false);
edtText.setLayoutParams(new LayoutParams(150,50));
linearLayout.addView(edtText);
Button button = new Button(this);
button.setText("say hello");
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
EditText edt = null;
for(int i=0; i<linearLayout.getChildCount();i++){
Class<?> c = (Class<?>) linearLayout.getChildAt(i).getClass();
if(c.getName().equals("android.widget.EditText")){
edt = (EditText) linearLayout.getChildAt(i);
}
}
edt.setText("hello");
}
});
【问题讨论】:
-
呵呵,没用。我一开始就这么试过。如果这么简单,我不会浪费时间发布问题。
标签: android android-layout android-widget