小编Akm*_*iev的帖子

TextInputLayout中的AppCompatEditText.getpParent()返回FrameLayout

我正在创建简单的AppCompatEditText,添加OnFocusChangeListener并将其放在简单的TextInputLayout中.

当AppCompatEditText失去焦点时,它的内容应该通过isValidParam方法验证.

它工作到昨天,当我使用rev.23.0.3但是现在,当我使用rev.24.0.2时,它在isValidParam方法的第1行给出如下错误.

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

我检查了调试模式.AppCompatEditText.getpParent()确实返回Framelayout而不是TextInputLayout.

LinearLayout llParams = new LinearLayout(context);
llParams.setOrientation(LinearLayout.VERTICAL);

// Create label for param
final TextInputLayout tilParam = new TextInputLayout(context);
// Add label into layout
llParams.addView(tilParam);


// Create Editor for param
final AppCompatEditText etParam = new AppCompatEditText(context);

edParam.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus)
            if (isValidParam(etParam)) {
                do some thing;
            } else {
                do other thing;
            }
    }
});

tilParam.addView(etParam);


// validation method
boolean isValidParam(AppCompatEditText editText) {
    TextInputLayout til = (TextInputLayout) editText.getParent(); …
Run Code Online (Sandbox Code Playgroud)

android android-appcompat android-design-library android-textinputlayout textinputlayout

12
推荐指数
3
解决办法
3266
查看次数