【问题标题】:How to get text from edittext of inflated LinerLayout如何从膨胀的LinearLayout的edittext中获取文本
【发布时间】: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


【解决方案1】:

在您的父级mainLayout 上使用findViewById() 方法来查找您的子级线性布局。比在它上面找到具有相同逻辑的子元素。每个视图都有其子视图的 findViewById() 方法。

【讨论】:

    【解决方案2】:

    试试这个,

    ArrayList<String> edittextValues = new ArrayList<String>();
    for(int i = 0; i < mainLayout.getChildCount(); i++){
        View view = (View)mainLayout.getChildAt(i);
        EditText editText = (EditText) view.findViewById(R.id.et_pn);
        edittextValues.add(editText.getText().toString());
    }
    

    【讨论】:

    • 在最后一行得到nullPointerException。意味着 edittext 没有到达那里。
    • 还是一样。
    【解决方案3】:

    您在 IMO 的问题是,您尝试创建自己的列表,而不是使用 ListView,并附加每个 View。

    我提出的解决方案: 使用 ListView 和 ArrayAdapter。对于ArrayAdapter 中的每一行,您将能够访问EditText 和ImageButton。

    编辑
    根据我们的讨论,由于ListView 的回收属性,用户输入的文本在添加行后没有保存和丢失,您应该自定义您的ArrayAdapter 以保存用户输入的文本。 这可以在ArrayAdapter 的getView 中完成,如下所述:https://stackoverflow.com/a/9441210/891479。

    【讨论】:

    • 我已经尝试过这种方法。但在那种情况下,当我添加新项目时,我无法保留以前的 EditText 的值。如果有工作示例,那么我可以再次使用它。
    • 也许我错过了一些东西,但您应该能够访问 ArrayAdapter 构造函数 ArrayAdapter(Context context, int resource, int textViewResourceId, List objects) 中给出的 Object List 的所有值。您还可以自定义 ArrayAdapter 以匹配您的数据模型。
    • 当我将新项目添加到 ArrayAdapter 时,用户已经输入的值应该保留在视图中。但是当我使用适配器更新视图时,EditText 的那些文本被清除了。
    • 我认为它是指向 ListView 回收的链接,其他问题的原因和解决方案可能在这里相同:stackoverflow.com/questions/9438676/…。基本上你需要做的就是将 EditTexts 的输入保存在一些数据结构/对象列表或数组中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    相关资源
    最近更新 更多