【发布时间】:2014-07-12 10:28:22
【问题描述】:
我有一个LinearLayout(比如父母),我正在膨胀另一个LinearLayout(比如孩子),其中包含EditText 和ImageButton。
单击主布局中的按钮时,我会膨胀子布局。当单击子布局的ImageButton 时,该布局被删除。我已经设置好了。
现在我想获取子布局的所有EditText 的文本,当单击ActionBar 的保存按钮时。
以下是我用来扩大我的观点的方法。 mainLayout 是我的父布局。现在如何在我的活动中的任何地方获取所有添加的 EditText 的文本?
private void addEditView(final LinearLayout mainLayout) {
LayoutInflater inflater = LayoutInflater.from(this);
View theInflatedView = inflater.inflate(R.layout.layout_add_phone_number_listview, null);
//Get the EditText in you xml to set the new id
EditText editText = (EditText) theInflatedView.findViewById(R.id.et_pn);
editText.setId(4000+1);//mainLayout.getChildCount()
ImageButton remove = (ImageButton) theInflatedView.findViewById(R.id.btn_remove_raw);
remove.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mainLayout.removeView((View)v.getParent());
}
});
//add the view into the LinearLayout (global variable)
mainLayout.addView(theInflatedView);
}
我尝试了什么:
LinearLayout ll = (LinearLayout)findViewById(R.id.phone_info);
int count = ll.getChildCount();
for(int i =0;i<count;i++)
{
Log.e("child count:", "count:" + count + "ID:" + ll.getChildAt(i).getId());
View vi = ll.getChildAt(i);
if(vi instanceof EditText)
{
// you got the spinner
EditText s = (EditText) findViewById(4000+i);
Log.e("Item selected",s.getText().toString());
}
}
【问题讨论】:
-
你试过了吗 String text = editText.getText().toString();
-
setId需要什么? -
@Raghunandan :如果不需要,我会删除它。我只想要 eddittext 的文本。那个 ID 现在无关紧要
-
@I-droid 将此
EditText editText声明为实例变量。将 EditText 初始化为editText = (EditText) theInflatedView.findViewById(R.id.et_pn);然后editText.getText().toString() -
@Raghunandan:当我有更多
EditText时,这会起作用吗???
标签: android android-layout android-edittext layout-inflater