【问题标题】:How to get the TextInputLayout related to edit text?如何获取与编辑文本相关的 TextInputLayout?
【发布时间】:2017-08-04 18:34:33
【问题描述】:

我在不同的 TextInputLayouts 中有许多编辑文本,我如何获得 TextInputLayout 我正在使用的编辑文本在哪里?

例子:

@BindView(R.id.eT)
EditText eT;

@BindView(R.id.eT2)
EditText eT2;

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

@BindView(R.id.text_input_layout2)
TextInputLayout textInputLayout2;

我想要类似 eT.getTextInputLayout 并获取 textInputLayout

xml 的提取:

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

                <Spinner
                    android:id="@+id/spinner"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAlignment="center" />

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/text_input_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"
                    app:errorTextAppearance="@style/error_appearance"
                    app:hintAnimationEnabled="true"
                    app:errorEnabled="true">

                <EditText
                    android:id="@+id/eT"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:inputType="number" />

                </android.support.design.widget.TextInputLayout>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="horizontal"
                tools:layout_editor_absoluteX="8dp"
                tools:layout_editor_absoluteY="0dp">

                <TextView
                    android:id="@+id/tV"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:labelFor="@+id/spinne2"
                    android:text="@string/tarjeta" />

                <Spinner
                    android:id="@+id/spinner2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAlignment="center" />

            </LinearLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/text_input_layout2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"
                app:errorTextAppearance="@style/error_appearance"
                app:hintAnimationEnabled="true"
                app:errorEnabled="true">

                <EditText
                    android:id="@+id/eT2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:ems="10"
                    android:inputType="number" />
            </android.support.design.widget.TextInputLayout>
    </LinearLayout>

我想通过编辑文本 eT 获取例如 text_input_layout 以处理 TIL 中的错误(我在其他模块中执行此操作,因此我不想将编辑文本和 TIL 传递给此模块)。

【问题讨论】:

  • 您可以尝试调用 eT.getParent() 并对其进行转换,但正如 TextInputLayout 所写的那样“TextInputLayout 下的实际视图层次结构不能保证与以 XML 编写的视图层次结构相匹配。因此, 在 TextInputLayout 的子项(例如 TextInputEditText)上调用 getParent() 可能不会返回 TextInputLayout 本身,而是返回中间视图。如果您需要直接访问视图,请设置 android:id 并使用 findViewById(整数)。”
  • 你可以发布布局文件吗?
  • 完成 @Mani 我正在考虑使用编辑文本创建诸如散列之类的东西 => TIL 所以我将散列传递给另一个模块...
  • 为什么不采用@yasin 的方法?它与您当前的布局结构更清洁的方法
  • @Mani 因为我需要 TIL 来制作错误消息(如 textInputLayout.setError("ERROR") 当我发现错误时。在“子”编辑文本中,所以我需要这种关系和如果我理解正确,getParent 不保证返回 TIL。我在另一个模块中所做的是接收编辑文本和 TIL 列表,所以当 eT 发现错误时,调用 setError,我不想通过 findViewById 声明所以我可以将此方法与不同的布局一起使用

标签: android android-layout android-edittext butterknife android-textinputlayout


【解决方案1】:

@Sami-Issa是对的

edittext.getParent().getParent() 

返回文本输入布局

【讨论】:

    【解决方案2】:

    注意:TextInputLayout 下的实际视图层次结构不是 保证与用 XML 编写的视图层次结构相匹配。因此, 在 TextInputLayout 的子级上调用 getParent() - 例如 TextInputEditText -- 可能不会返回 TextInputLayout 本身,但 而是一个中间视图。如果您需要直接访问视图, 设置一个 android:id 并使用 findViewById(int)。 https://developer.android.com/reference/android/support/design/widget/TextInputLayout

    但如果你还想这样做,你可以使用这样的方法:

    private static TextInputLayout findTextInputLayoutParent(View view) {
        return findTextInputLayoutParent(view.getParent(), 3);
    }
    
    private static TextInputLayout findTextInputLayoutParent(ViewParent view, int maxDepth) {
        if (view.getParent() instanceof TextInputLayout) {
            return (TextInputLayout) view.getParent();
        } else if (maxDepth > 0){
            return findTextInputLayoutParent(view.getParent(), maxDepth -1);
        }
        return null;
    }
    

    【讨论】:

      【解决方案3】:

      如果该视图位于 文本输入布局

      【讨论】:

      • 不起作用。对于每个 TextView,getParentForAccessibility 返回一个 Layout 组件(FrameLayout 或 ScrollLayout)
      • @SamiIssa。这就是为什么我的回答说“如果该视图位于 TextInputLayout 内”。这意味着有问题的视图的直接父级必须是 TextInputLayout 才能使此解决方案起作用
      • 我有一个 FrameLayout,其中包含一个 TextInputLayout,它包含一个 TextInputEditText。当我调用 TextInputLayout.getParentForAccessibility() 时,它返回 FrameLayout。最后我用一种非优雅的方式解决了,但是有效。 TextInputLayout.getParent().getParent()。我并不为此感到自豪:)
      • @zulkarnain 请举例说明如何在扩展的 EditText 中使用 getParentForAccessibility 获取 TextInputLayout!谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 2017-09-18
      • 2022-12-04
      • 1970-01-01
      • 2015-06-14
      • 2013-10-25
      • 1970-01-01
      相关资源
      最近更新 更多