【问题标题】:Adding TextInputLayout and EditText dynamically (binding)动态添加 TextInputLayout 和 EditText(绑定)
【发布时间】:2017-06-25 04:00:22
【问题描述】:

我想在 TextInputLayout 中添加一个新的 EditText,就像在 xml 文件中静态添加它们一样。 (所以一开始有两个 TextInputLayouts,当用户按下按钮时,它应该添加新的)。我看到了其他问题,我知道这是关于父布局的,但我无法让它工作。顺便说一句,我正在使用绑定。就我而言,我认为父级应该是 ScrollView。对于EditText,我尝试了RelativeLayout 和LinearLayout,但无论如何都会出错。

这里是xml文件。

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollView">

        <RelativeLayout
            android:id="@+id/activity_multi_opt_poll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            tools:context="com.xyz.MyApp.EntryActivity">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/nameInputLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Name">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/nameEditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/questionInputLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/nameInputLayout"
                android:hint="Question">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/questionEditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/opt1InputLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/questionInputLayout"
                android:hint="1. Option">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/opt1EditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </android.support.design.widget.TextInputLayout>


            <android.support.design.widget.TextInputLayout
                android:id="@+id/opt2InputLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/opt1InputLayout"
                android:hint="2. Option">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/opt2EditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.FloatingActionButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                app:fabSize="normal"
                app:srcCompat="@android:drawable/ic_input_add"
                android:id="@+id/addFAB"
                android:layout_marginEnd="21dp"
                android:layout_below="@+id/opt2InputLayout"
                android:layout_alignParentEnd="true" />
        </RelativeLayout> 
    </ScrollView>
</layout>

以及新增功能

public class EntryActivity extends AppCompatActivity {
    private ActivityEntryBinding binding;
    private List optionsViews = new ArrayList();
    private int count = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_entry);


        binding.addFAB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                binding.scrollView.addView(createNewOptionEntry());

            }
        });
    }

    private TextInputLayout createNewOptionEntry() {

        ScrollView.LayoutParams lparams = new ScrollView.LayoutParams(
                ScrollView.LayoutParams.MATCH_PARENT, ScrollView.LayoutParams.MATCH_PARENT);

        TextInputLayout textInputLayout = new TextInputLayout(this);
        textInputLayout.setLayoutParams(lparams);
        textInputLayout.setHint(count++ + ". Option");


        LinearLayout.LayoutParams lparams2 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        final TextInputEditText editText = new TextInputEditText(this);
        editText.setLayoutParams(lparams2);
        int id = View.generateViewId();
        /*to be able to get dynamically generated editText's input*/
        optionsViews.add(id);
        editText.setId(id);

        textInputLayout.addView(editText, lparams);

        return textInputLayout;
    }


}

在 textInputLayout.addView(editText, lparams) 行给我错误:

 E/AndroidRuntime: FATAL EXCEPTION: main
 Process: mattoncino.pollo, PID: 23572
 java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
   at android.support.design.widget.TextInputLayout.updateInputLayoutMargins(TextInputLayout.java:371)
   at android.support.design.widget.TextInputLayout.addView(TextInputLayout.java:271)
   at android.view.ViewGroup.addView(ViewGroup.java:3540)
   at mattoncino.pollo.MultiOptPollActivity.createNewOptionEntry(MultiOptPollActivity.java:95)
   at mattoncino.pollo.MultiOptPollActivity.access$000(MultiOptPollActivity.java:27)
   at mattoncino.pollo.MultiOptPollActivity$1.onClick(MultiOptPollActivity.java:48)
   at android.view.View.performClick(View.java:4463)
   at android.view.View$PerformClick.run(View.java:18770)
   at android.os.Handler.handleCallback(Handler.java:808)
   at android.os.Handler.dispatchMessage(Handler.java:103)
   at android.os.Looper.loop(Looper.java:193)
   at android.app.ActivityThread.main(ActivityThread.java:5333)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
   at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • @pskink 它仍然给出同样的错误。
  • 好吧,我错了,忘了我说的话,老实说我不知道​​你为什么在这里有FrameLayout.LauoutParams,奇怪....你试过ViewGroup.LayoutParams吗?
  • @pskink,是的,无论我更改 lparams2,它都会给出相同的错误。所以问题可能是别的……

标签: android android-layout android-textinputlayout


【解决方案1】:

好的,现在我想我解决了布局问题。 1)TextInputLayout 的父级有 FrameLayout,我从父级获得它。 2)EditText的父类是TextnputLayout,它是LinearLayout的子类,所以EditText应该将其布局设置为LinearLayout。 我觉得这部分还可以。但现在它给了我一个错误,说 ScrollView 只能托管一个直接的孩子。所以我实际上应该将它添加到RelativeLayout?

    private TextInputLayout createNewOptionEntry() {

        View parent = (View) binding.opt1InputLayout.getParent();

        FrameLayout.LayoutParams fLayout  = (FrameLayout.LayoutParams) parent.getLayoutParams();

        TextInputLayout textInputLayout = new TextInputLayout(this);
        textInputLayout.setLayoutParams(fLayout);
        textInputLayout.setHint(count++ + ". Option");

        LinearLayout.LayoutParams lLayout = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        final TextInputEditText editText = new TextInputEditText(this);
        editText.setLayoutParams(lLayout);
        int id = View.generateViewId();
        optionsViews.add(id);
        editText.setId(id);

        textInputLayout.addView(editText);

        return textInputLayout;
    }

【讨论】:

    猜你喜欢
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多