如何处理这类事件

时间:2016-01-01 10:37:01

标签: android android-fragments android-cardview

我想制作一个这样的按钮。您可以在其中添加更多表单。

enter image description here

这些类型的表格将生成。

enter image description here

如何在android中创建和处理这种类型的事件。这张图片取自ios,但我想在android中实现它。

提前致谢。抱歉英语不好。

1 个答案:

答案 0 :(得分:0)

请按照以下步骤操作:

1) take hidden Layout. 
2) Make one layout that include your Button ,    Editext.. etc. 
3) When Click on Plus Button inflate layout & add it to hidden layout.

使用此代码:

LayoutInflater inflater = LayoutInflater.from(context);
View mView = (View) inflater.inflate(R.layout.custom_layout, null, false);

// Access button & Edittext like:
Button mButton= (Button)mView.findViewById(R.id.mButton);
EditText mEdiText= (EditText)mView.findViewById(R.id.mEditText);

LinearLayout hiddenLinear = (LinearLayout)findViewById(R.id.myLayout);
hiddenLinear.addView(mView);

修改

添加多个EditText使用 FOR 循环

  LinearLayout hiddenLinear = (LinearLayout)findViewById(R.id.myLayout);

        for (int i = 0, l <= 3; i++) 
       {
        LayoutInflater inflater = LayoutInflater.from(context);
        View mView = (View) inflater.inflate(R.layout.custom_layout, null, false);

        // Access button & Edittext like:
        Button mButton= (Button)mView.findViewById(R.id.mButton);
        EditText mEdiText= (EditText)mView.findViewById(R.id.mEditText);
         hiddenLinear.addView(mView);
        }

&安培;从All View获取所有EditText值使用此方法:

LinearLayout hiddenLinear = (LinearLayout)findViewById(R.id.myLayout);
findAllEdittexts(hiddenLinear);


 ArrayList<EditText> array = new ArrayList<>();

    private void findAllEdittexts(ViewGroup viewGroup) {

        int count = viewGroup.getChildCount();
        for (int i = 0; i < count; i++) {
            View view = viewGroup.getChildAt(i);
            if (view instanceof ViewGroup)
                findAllEdittexts((ViewGroup) view);
            else if (view instanceof EditText) {
                EditText edittext = (EditText) view;
                array.add(edittext);
            }
        }
    }