【问题标题】:EditText is not getting focus in Custom Compound ViewEditText 没有在自定义复合视图中获得焦点
【发布时间】:2019-07-24 06:40:28
【问题描述】:

我开发了一个复合视图,其中包含 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>

【问题讨论】:

  • 删除android:cursorVisible="false"到Edittext标签

标签: android android-layout android-edittext android-custom-view


【解决方案1】:

我遇到了同样的问题。

不知道为什么,但是注释掉输入类型:“TYPE_TEXT_VARIATION_NORMAL”解决了这个问题。 看看它是否适合你。

【讨论】:

    【解决方案2】:

    使用android:cursorVisible="false"是为了删除EditText闪烁光标所以使用android:cursorVisible="true"。如果您想以编程方式添加它,请使用此mEditText.setCursorVisible(true)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-20
      • 2023-03-08
      • 2015-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多