回收器视图中的CustomView在聚焦时不显示键盘

时间:2019-03-15 20:54:32

标签: android-recyclerview android-edittext android-custom-view android-softkeyboard android-inputtype

我创建了一个扩展constraintsalyout的自定义视图。在该视图中,我添加了EditText和Textview。当我尝试在回收器视图中添加此自定义视图时,当我触摸该edittext时,它不会显示键盘。

在回收者视图之外添加时,它完美工作

public class SVEditText extends ConstraintLayout {

private Context context;
private EditText editText;
private TextView textView;

public SVEditText(final Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.sv_edittext, this);
    editText = view.findViewById(R.id.editText);
    textView = view.findViewById(R.id.textView);

    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SVEditText);
    editText.setText(typedArray.getString(R.styleable.SVEditText_text));
    editText.setHint(typedArray.getString(R.styleable.SVEditText_hint));
    editText.setLines(typedArray.getInteger(R.styleable.SVEditText_android_lines, 1));
    editText.setInputType(typedArray.getInteger(R.styleable.SVEditText_android_inputType, 0));
    textView.setText(typedArray.getString(R.styleable.SVEditText_title));
    setDrawablesToEditText(typedArray);

    typedArray.recycle();
}

private void setDrawablesToEditText(TypedArray typedArray){
    if (typedArray.getInteger(R.styleable.SVEditText_android_lines, 1) != 1){
        Drawable innerDrawableLeft = typedArray.getDrawable(R.styleable.SVEditText_iconLeft);
        Drawable innerDrawableRight = typedArray.getDrawable(R.styleable.SVEditText_iconRight);
        GravityCompoundDrawable iconLeft = null, iconRight = null;
        if (innerDrawableLeft != null) {
            iconLeft = new GravityCompoundDrawable(innerDrawableLeft);
            innerDrawableLeft.setBounds(0, 30, innerDrawableLeft.getIntrinsicWidth(), innerDrawableLeft.getIntrinsicHeight()+30);
            iconLeft.setBounds(0, 0, innerDrawableLeft.getIntrinsicWidth(), innerDrawableLeft.getIntrinsicHeight());
            iconLeft.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(getContext(), R.color.colorPrimary), PorterDuff.Mode.SRC_IN));
        }
        if (innerDrawableRight != null) {
            iconRight = new GravityCompoundDrawable(innerDrawableRight);
            innerDrawableRight.setBounds(0, 30, innerDrawableRight.getIntrinsicWidth(), innerDrawableRight.getIntrinsicHeight()+30);
            iconRight.setBounds(0, 0, innerDrawableRight.getIntrinsicWidth(), innerDrawableRight.getIntrinsicHeight());
            iconRight.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(getContext(), R.color.colorPrimary), PorterDuff.Mode.SRC_IN));
        }

        editText.setCompoundDrawablesWithIntrinsicBounds(iconLeft,
                null, iconRight, null);
        editText.setBackgroundResource(R.drawable.bg_edit_multiline);
        return;
    }

    editText.setCompoundDrawablesWithIntrinsicBounds(typedArray.getDrawable(R.styleable.SVEditText_iconLeft),
            null, typedArray.getDrawable(R.styleable.SVEditText_iconRight), null);
}

public void setText(String value){
    editText.setText(value);
}

public void setHint(String hint){
    editText.setHint(hint);
}

public void setTitle(String title){
    textView.setText(title);
}

public void setCompoundDrawablesWithIntrinsicBounds( @DrawableRes int left, @DrawableRes int top, @DrawableRes int right, @DrawableRes int bottom){
    editText.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
}

public void setText(Spannable spannable) {
    editText.setText(spannable);
}
}

1 个答案:

答案 0 :(得分:0)

最后,我发现了我犯的错误。 我错过了xml中的这一行。

android:inputType="phone"

因此从我的自定义视图代码中将其默认为0。 我将该默认值更新为1,现在它可以正常工作,而无需在xml中添加inputtype。