Recyclerview内的自定义视图在焦点时不显示键盘

问题描述 投票:0回答:1

我创建了一个扩展约束的自定义视图。在该视图中,我添加了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);
}
}
android-recyclerview android-edittext android-custom-view android-softkeyboard android-inputtype
1个回答
0
投票

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

android:inputType="phone"

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

© www.soinside.com 2019 - 2024. All rights reserved.