【问题标题】:How to add TextInputLayout inside the LinearLayout by dynamically?如何在 LinearLayout 中动态添加 TextInputLayout?
【发布时间】:2021-04-09 12:57:26
【问题描述】:

我想在一个线性布局中动态添加 TextInputLayout。我可以在 linearLayout 中添加 TextView 和 EditText,但不能添加 TextInputLayout。 那是我的线性布局:

  <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:orientation="vertical"
               android:id="@+id/userInformation_lnr">

我可以使用以下代码块添加 TextView 和 EditText

LinearLayout ll = (LinearLayout) findViewById(R.id.userInformation_lnr);
        for(int i = 0; i <oyuncuSayisi; i++ ){
            EditText et = new EditText(this);
            TextView tx = new TextView(this);
            tx.setText("Player "+(i+1));
            tx.setTextSize(19);
            tx.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD_ITALIC));
            tx.setTextColor(this.getResources().getColor(R.color.silver));

            et.setHint((i+1)+".Enter Player Name");
            et.setHintTextColor(this.getResources().getColor(R.color.DarkGoldenrod));
            DrawableCompat.setTint(et.getBackground(), ContextCompat.getColor(this, R.color.silver));
            et.setTextColor(this.getResources().getColor(R.color.SteelBlue));
            et.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD_ITALIC));
            et.setTextSize(17);
            et.setId((i+1));
            ll.addView(tx);
            ll.addView(et);
        }

但我不知道如何添加TextInputLayout,如何在LinearLayout里面添加TextInputLayout?

【问题讨论】:

  • 同理。你有什么问题?
  • 不知道怎么添加TextInputLAyout
  • 您当然可以将 TextInputLayout 添加到 LinearLayout 中,但 TextInputLayout 仅在具有 TextInputEditText 的子项时才有效。因此,将 TextInputLayout 添加到您的 LinearLayout,并将 TextInputEditText 添加到 TextInputLayout。也许这是你的问题?
  • 我添加了 TextInputLayout 但如何添加样式?

标签: android android-linearlayout android-textinputlayout


【解决方案1】:

如果您将演示文稿移动到 xml 文件,您可以将演示文稿和业务逻辑解耦。

input_layout.xml

<com.google.android.material.textfield.TextInputLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:theme="@style/MyInputTheme"    <-- You can define here your theme or style
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

并由LayoutInflater 创建:

LinearLayout container = (LinearLayout) findViewById(R.id.container);
LayoutInflater.from(context).inflate(R.layout.input_layout, container, true);

【讨论】:

  • 感谢您的建议,但我有一个问题。当我使用您的方法时,如何从 TexInputLayut 获取输入?如何以这种方式将 Id 设置为 TextInputLayout?
  • @Neo 您可以通过方法setId() 将 id 设置为TexInputLayut 以用于任何其他视图。 EditTextTexInputLayut 的唯一直系子代,所以你可以通过getChildAt(0) 方法得到它
  • 我创建了一个名称播放器的 xml。我在 player.xml 添加 TextInputLayout 。然后我在 for 循环中膨胀 player.xml 以创建用户想要的尽可能多的 textinputlayout。好吧,为了从我创建的 textinputlayouts 中获取用户输入,我必须为 textinputlayouts 提供一个 ID。膨胀时如何将此 ID 传递给 textinputlayouts。
  • @Neo 你不能传递 id。您可以像往常一样在 xml 中定义它,也可以在视图创建(膨胀)时分配它
  • 当我使用inflate创建时,用户创建了多个TextInputLayout,如何获取用户在TextInputLayout中输入的信息?
【解决方案2】:

TextInputLayout 需要一个 TextInputEditText 才能工作,您可以为您的 TextInputLayout 制作一个自定义视图,然后将其添加到 LinearLayout。

<com.google.android.material.textfield.TextInputLayout 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"
    android:id="@+id/tv_input_layout"
    style="@style/YourCustomStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:errorEnabled="false"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:hint="hint"
    tools:placeholderText="ex: hint">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/tv_edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="value" />

</com.google.android.material.textfield.TextInputLayout>

班级:

class CustomTextInputLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : TextInputLayout(context, attrs, defStyleAttr) {

    init {
        View.inflate(context, R.layout.custom_input_layout, this)
        // set the other things you need
    }
}

然后将其添加到线性布局中:

private fun createInput() {
        val view = CustomTextInputLayout(context)

        ll.addView(view)
    }
}

【讨论】:

  • 感谢您的建议,但我不知道 Kotlin :( 我使用 java 语言开发我的项目
【解决方案3】:

您必须在 TextInputEditText 中添加 TextInputLayout
比如:

    LinearLayout myLayout = findViewById(R.id.userInformation_lnr);
    TextInputLayout til = new TextInputLayout(this);
    til.setHint("Label");
    TextInputEditText et = new TextInputEditText(til.getContext());
    til.addView(et);
    myLayout.addView(til);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多