从子textInputEditText获取父texInputlayout

Rah*_*rma 6 android android-design-library android-textinputlayout android-textinputedittext

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

为此,我正在使用OnFocusChangeListener它的孩子textInputEditText.为了便于实现,我正在实施View.OnFocusChangeListener我的活动,如:

public class LoginActivity extends BaseActivity implements View.OnFocusChangeListener
Run Code Online (Sandbox Code Playgroud)

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

@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()));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在上面的方法中,我试图textInputLayout使用该行获取父视图

TextInputLayout textInputLayout = (TextInputLayout) findViewById(v.getId()).getParent();
Run Code Online (Sandbox Code Playgroud)

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

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

它非常明显,因为它返回了Framelayout无法投入的内容textInputLayout

如果我使用

TextInputLayout textInputLayout = (TextInputLayout) findViewById(v.getId()).getRootView();
Run Code Online (Sandbox Code Playgroud)

它再次抛出一个致命的错误,因为无法投入的getRootView()回报DecorViewtextInputLayout

我的问题是如何textInputLayout从孩子那里得到父母textInputEditText

请指导.

Rah*_*rma 10

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

TextInputLayout textInputLayout = (TextInputLayout) findViewById(v.getId()).getParent().getParent();
Run Code Online (Sandbox Code Playgroud)

textInputlayout根据需要返回.