从子textInputEditText获取父texInputlayout [英] Get parent texInputlayout from child textInputEditText

本文介绍了从子textInputEditText获取父texInputlayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am implementing a functionality to change the case of textInputlayout Hint text to upper case when the hint floats up and vice versa.

为此,我对其子项 textInputEditText 使用 OnFocusChangeListener 。为了易于实施,我在活动上实现了 View.OnFocusChangeListener ,例如:

For that I am using OnFocusChangeListener on its child textInputEditText. To make it easy to implement I am implementing View.OnFocusChangeListener on my activity like:

public class LoginActivity extends BaseActivity implements View.OnFocusChangeListener

并覆盖活动中的方法,例如:

and overriding the method in the activity like:

@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()));
        }
    }
}

在上述方法中,我是尝试使用

In the above method I am trying to get the view of parent textInputLayout using the line

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

上面的代码行会引发致命错误

The above line of code throws a fatal error


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

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

这很明显,因为它返回 Framelayout ,该值不能在 textInputLayout

and it is very obvious because it returns Framelayout which cannot be casted in textInputLayout

如果我使用

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

它再次引发致命错误,因为 getRootView()返回 DecorView ,该值不能在 textInputLayout

it again throws a fatal error because getRootView() returns DecorView which cannot be casted in textInputLayout

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

My question is how to get parent textInputLayout from child textInputEditText?

请指导。

推荐答案

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

I solved the problem with the below line of code:

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

它会根据需要返回 textInputlayout

这篇关于从子textInputEditText获取父texInputlayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆