android - 从子 textInputEditText 获取父 textInputlayout

标签 android android-design-library android-textinputlayout android-textinputedittext

我正在实现一项功能,以将 textInputlayout 提示文本的大小写更改为大写 当提示向上 float 时,反之亦然。

为此,我在其子 textInputEditText 上使用 OnFocusChangeListener。为了便于实现,我正在对我的 Activity 实现 View.OnFocusChangeListener,例如:

public class LoginActivity extends BaseActivity implements View.OnFocusChangeListener

并覆盖 Activity 中的方法,例如:

@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 的 View
TextInputLayout textInputLayout = (TextInputLayout) findViewById(v.getId()).getParent();

上面的代码行抛出一个 fatal error

java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.support.design.widget.TextInputLayout

而且非常明显,因为它返回的 Framelayout 不能在 textInputLayout 中转换

如果我使用

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

它再次抛出一个 fatal error ,因为 getRootView() 返回不能在 textInputLayout 中转换的 DecorView

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

请指导。

最佳答案

我用下面的代码行解决了这个问题:

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

它根据需要返回 textInputlayout

关于android - 从子 textInputEditText 获取父 textInputlayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44883544/

相关文章:

android - 在 TextInputLayout 中更改单个字母或字母表的提示颜色

安卓 repo 变更日志

android - Activity 返回转换 - 共享元素 - 被杀死的 Activity

android - Eclipse 中的 "Design"库即使使用 API 21 也无法编译

android - 如何自定义NavigationView中的菜单项,如GMail App

android - 使用 TextWatcher 在 EditText 中设置最大字数限制

android - EditText TextInputLayout 提示文字对齐 : Is there a way to align hint text like textAlignment property of EditText view

android - 在 Android 中将城市作为标记传递

android - ScrollView 中的捏缩放功能

java - 如何将 XML 文件传递​​给 XMLReader