代码之家  ›  专栏  ›  技术社区  ›  Usman Rana

EditText在自定义复合视图中未获得焦点

  •  0
  • Usman Rana  · 技术社区  · 5 年前

    我开发了一个复合视图,其中包含TextView、Imageview和EditText。我面临的问题是,EditText无法在点击时显示键盘焦点,光标也不可见。我错过了什么?我也尝试过requestFocus和focusOnTouchMode,但都没用。代码如下:

    public class ExtendedInputField extends FrameLayout {
    
        private LayoutExtendedInputFieldBinding itemViewBinding;
    
        public ExtendedInputField(Context context) {
            super(context);
            initializeView(context, null, 0);
        }
    
        public ExtendedInputField(Context context, AttributeSet attrs) {
            super(context, attrs);
            initializeView(context, attrs, 0);
        }
    
        public ExtendedInputField(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            initializeView(context, attrs, defStyleAttr);
        }
    
        private void initializeView(Context context, AttributeSet attrs, int defStyleAttr) {
    
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ExtendedInputField, defStyleAttr, 0);
            String hint = null, title = null, text = null;
            int inputType = EditorInfo.TYPE_TEXT_VARIATION_NORMAL, imeOptions = -1, lines = 1;
            boolean singleLine = true;
            Drawable labelIcon = null;
    
            try {
                int n = a.getIndexCount();
                for (int i = 0; i < n; i++) {
                    int attr = a.getIndex(i);
                    switch (attr) {
                        case R.styleable.ExtendedInputField_android_hint:
                            hint = a.getString(attr);
                            break;
                        case R.styleable.ExtendedInputField_android_inputType:
                            inputType = a.getInt(attr, EditorInfo.TYPE_TEXT_VARIATION_NORMAL);
                            break;
                        case R.styleable.ExtendedInputField_android_imeOptions:
                            imeOptions = a.getInt(attr, 0);
                            break;
                        case R.styleable.ExtendedInputField_android_lines:
                            lines = a.getInt(attr, 0);
                            break;
                        case R.styleable.ExtendedInputField_android_singleLine:
                            singleLine = a.getBoolean(attr, true);
                            break;
                        case R.styleable.ExtendedInputField_android_text:
                            text = a.getString(attr);
                            break;
                        case R.styleable.ExtendedInputField_title:
                            title = a.getString(attr);
                            break;
                        case R.styleable.ExtendedInputField_iconDrawable:
                            labelIcon = a.getDrawable(attr);
                            break;
                        default:
                            Log.d("ExtendedInputField", "Unknown attribute for " + getClass().toString() + ": " + attr);
                            break;
                    }
                }
    
    
            } finally {
                a.recycle();
            }
    
    
            itemViewBinding = LayoutExtendedInputFieldBinding.inflate(LayoutInflater.from(context), this, true);
            if (labelIcon != null) {
                itemViewBinding.ivIcon.setImageDrawable(labelIcon);
            }
            if (!TextUtils.isEmpty(title))
                itemViewBinding.tvTitle.setText(title);
            if (!TextUtils.isEmpty(text))
                itemViewBinding.etInput.setText(text);
            if (!TextUtils.isEmpty(hint))
                itemViewBinding.etInput.setHint(hint);
            itemViewBinding.etInput.setInputType(inputType);
            itemViewBinding.etInput.setSingleLine(singleLine);
            itemViewBinding.etInput.setLines(lines);
            if (imeOptions != -1)
                itemViewBinding.etInput.setImeOptions(imeOptions);
    
        }
    
        @Override
        protected void onFinishInflate() {
            super.onFinishInflate();
            addEditTextListener();
        }
    
        private void addEditTextListener() {
            itemViewBinding.etInput.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    itemViewBinding.etInput.setCursorVisible(true);
                }
            });
        }
    
        public void setIcon(@DrawableRes int resId) {
            itemViewBinding.ivIcon.setImageResource(resId);
        }
    
        public void setTitle(@StringRes int resId) {
            itemViewBinding.tvTitle.setText(resId);
        }
    
        public void setTitle(String text) {
            itemViewBinding.tvTitle.setText(text);
        }
    
        public EditText getEditText() {
            return itemViewBinding.etInput;
        }
    
    
        public String getText() {
            return itemViewBinding.etInput.getText().toString();
        }
    
        public void setText(String text) {
            if (text != null)
                itemViewBinding.etInput.setText(text);
        }
    
    }
    

    以下是xml代码:

    <?xml version="1.0" encoding="utf-8"?>
    
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">
    
        <data>
    
    
        </data>
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <ImageView
                android:id="@+id/ivIcon"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:src="@drawable/icon_email"
                app:layout_constraintBottom_toBottomOf="@+id/tvTitle"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/tvTitle" />
    
            <TextView
                android:id="@+id/tvTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="11.3dp"
                android:fontFamily="@font/sfui_text_light"
                android:textColor="@color/black"
                android:textSize="@dimen/extended_input_field_title_size"
                app:layout_constraintStart_toEndOf="@+id/ivIcon"
                app:layout_constraintTop_toTopOf="parent"
                tools:text="Email" />
    
            <EditText
                android:id="@+id/etInput"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="12.4dp"
                android:background="@drawable/extended_input_field_bottom_line"
                android:fontFamily="@font/sfui_text_semibold"
                android:paddingBottom="13.6dp"
                android:textColor="@color/extended_input_field_text_color"
                android:textSize="@dimen/extended_input_field_text_size"
                android:cursorVisible="false"
                android:focusableInTouchMode="true"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/tvTitle"
                tools:text="Enter your email" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    </layout>
    
    0 回复  |  直到 5 年前
        1
  •  1
  •   Richard Miller    5 年前

    我也有同样的问题。

    不知道为什么,但是注释掉输入类型:“type_TEXT_VARIATION_NORMAL”修复了这个问题。 看看对你有用吗。

        2
  •  -1
  •   Faisal Russel    5 年前

    使用 android:cursorVisible="false" 用于删除编辑文本闪烁光标,因此使用 android:cursorVisible="true" 。如果要以编程方式添加,请使用 mEditText.setCursorVisible(true)