从子textInputEditText获取父textInputlayout

13

我正在实现一个功能,将 textInputLayout 的提示文本大小写切换到大写当提示上浮时,反之亦然。

为此,我在其子元素 textInputEditText 上使用了 OnFocusChangeListener。 为了更容易实现,我在我的活动中实现了 View.OnFocusChangeListener

public class LoginActivity extends BaseActivity implements View.OnFocusChangeListener

并在活动中重写该方法,例如:

@Override
public void onFocusChange(View v, boolean hasFocus) {
    if(findViewById(v.getId()) instanceof TextInputEditText){
        TextInputLayout textInputLayout = (TextInputLayout) findViewById(v.getId()).getParent();
        if(hasFocus){
            textInputLayout.setHint(textInputLayout.getHint().toString().toUpperCase());
        }else{
            textInputLayout.setHint(Utility.modifiedLowerCase(textInputLayout.getHint().toString()));
        }
    }
}
在上面的方法中,我试图通过以下行获取父级textInputLayout的视图
TextInputLayout textInputLayout = (TextInputLayout) findViewById(v.getId()).getParent();

上面的代码会抛出致命错误

java.lang.ClassCastException: android.widget.FrameLayout 无法转换为 android.support.design.widget.TextInputLayout

很明显,它返回了一个不能转换为 TextInputLayoutFrameLayout

而如果我使用

TextInputLayout textInputLayout = (TextInputLayout) findViewById(v.getId()).getRootView();

由于getRootView()返回不可转换为textInputLayoutDecorView,因此它再次引发致命错误。

我的问题是如何从子项textInputEditText获取父级textInputLayout

请指导。


为什么不能像TextInputEditText一样在xml布局中为TextInputLayout分配一个id? - Daniel RL
@DanielRL 我已经在xml中分配了id,但是在我的重写方法中硬编码id只适用于那个textinputlayout,而不适用于其他的。我希望它能够适用于布局中的每个textinputlayout - Rahul Sharma
3个回答

18

我使用以下代码解决了问题:

TextInputLayout textInputLayout = (TextInputLayout) findViewById(v.getId()).getParent().getParent();

它会按要求返回textInputlayout


有没有与TextInputLayout相反的解决方案,可以获取第一个子元素(TextInputEditText)? - peterzinho16
1
@peterzinho16 你可以使用 TextInputLayout.getEditText() - zihadrizkyef
非常感谢@zihadrizkyef,我不知道那个方法。 - peterzinho16

3

不要使用 TextInputLayout textInputLayout = (TextInputLayout) findViewById(v.getId()).getParent(); 这种方式,而是使用视图实例的方式,例如 TextInputEditText et = (TextInputEditText) v; 然后再使用 TextInputLayout lay = (TextInputLayout)et.getParent().getParent();


0
您可以像这样在TextInputLayout中添加id:
<android.support.design.widget.TextInputLayout
    android:id="@+id/nameLayout"
    style="@style/LightInputLayoutNoBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/create_contact_account_data_name"
    app:hintTextAppearance="@style/TextSmall.Bold">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/name"
        style="@style/LightInputFieldNoBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableEnd="@drawable/ic_book_contacts" />

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

在活动中调用 findViewById 来获取 TextInputLayout。

我已经在XML中分配了ID,但是在这里硬编码ID在我的重写方法中只适用于该“textinputlayout”,而不适用于其他内容,我希望它适用于布局中的每个“textinputlayout”。 - Rahul Sharma
在Android文档中表示无法这样做:https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html因此,您必须通过TextInputEditText分配一个监听器并使用findViewById。 - Daniel RL

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接