【问题标题】:Add Defined TextInputLayout Dynamically and Programatically以动态和编程方式添加定义的 TextInputLayout
【发布时间】:2016-11-07 08:25:33
【问题描述】:

我正在尝试根据用户从 Spinner 中选择的数字添加带有 EditText 的 TextInputLayout。我已经在 XML 中定义了 TextInputLayout 属性,并希望简单地根据用户从微调器中选择的数字以编程方式添加它们:

XML:

<FrameLayout
    android:background="@drawable/image_border"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".525">

    <Button
        android:id="@+id/add_image_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Click to Add Image" />
</FrameLayout>

<LinearLayout
    android:orientation="vertical"
    android:padding="4dp"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".475">

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <EditText
            android:id="@+id/create_poll_question_editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:imeOptions="actionDone"
            android:singleLine="true"
            android:hint="@string/create_poll_question" />
    </android.support.design.widget.TextInputLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/how_many_answers_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/how_many_answers_text"
            android:textColor="@color/black"
            android:textSize="16sp" />

        <Spinner
            android:id="@+id/number_of_answers_spinner"
            android:layout_width="wrap_content"
            android:layout_gravity="bottom"
            android:layout_height="24dp"
            android:background="@android:drawable/btn_dropdown" />

    </LinearLayout>

  <!--Want to Add Programatically -->
    <android.support.design.widget.TextInputLayout
        android:id="@+id/create_poll_answer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <EditText
            android:id="@+id/create_poll_answer_editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:imeOptions="actionDone"
            android:singleLine="true"
            />
    </android.support.design.widget.TextInputLayout>

</LinearLayout>

这是我目前正在使用的代码,但它不会根据我已经创建的视图动态添加:

public class YourItemSelectedListener implements AdapterView.OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        String selected = parent.getItemAtPosition(pos).toString();
        Toast.makeText(getActivity().getApplicationContext(), selected, Toast.LENGTH_SHORT).show();
        for (int i = 0; i < Integer.parseInt(selected); i++) {
            ViewGroup layout = (ViewGroup) mRootView.findViewById(R.id.create_poll_linearlayout);
            EditText editText = new EditText(getActivity());
            editText.setHint("Poll Answer");
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            editText.setLayoutParams(layoutParams);
            TextInputLayout newAnswer = new TextInputLayout(getActivity());
            newAnswer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            newAnswer.addView(editText, layoutParams);
            layout.addView(newAnswer);
        }

    }

【问题讨论】:

  • 你还没有发布ID为create_poll_linearlayoutLinearLayout..?
  • TextInputLayout 和附上的EditText 放在单独的布局文件中。然后将其膨胀,并将其添加到您的ViewGroup

标签: java android xml material-design


【解决方案1】:

进行这种动态视图注入的最简单方法是使用 JakeWharton 的 ButterKnife 库

http://jakewharton.github.io/butterknife/

您可以像在活动的声明部分一样使用它:

@BindView(R.id.title) TextInputLayout textInputLayout;

然后将其绑定在 onCreate() 中,如下所示:

ButterKnife.bind(this);

这将大致相当于通过 id 膨胀和查找视图。

此外,该库有助于动态设置可绘制对象等。也很轻松

【讨论】:

    【解决方案2】:

    添加

    android:id="@+id/create_poll_linearlayout"

    到你的根 LinearLayout。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-07
      • 2017-03-11
      • 1970-01-01
      相关资源
      最近更新 更多