android - 在自定义 View 中禁用 EditText OnTouchListener 或 OnClickListener

标签 android android-edittext android-custom-view

我有一个自定义的 View,它包含一个 LinearLayout,而 LinearLayout 又包含一个 TextView 和一个 EditText。当我尝试点击我的自定义 View 时,如果我按下了 TextViewLinearLayout 将收到点击,但如果我按下了 EditText 它不会。

下面是我的 xml 的简化版本。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/edt_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/md_green_800" />

</LinearLayout>

因此,当我按下 EditText 时,我希望父 View (LinearLayout)接收点击。

我试过下面的代码:

private void init(Context context, AttributeSet attrs) {
        LayoutInflater inflater = LayoutInflater.from(context);
        binding = LayoutHeadingEdittextBinding.inflate(inflater, this, true);
        TypedArray array = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.headingtext,
                0, 0);
        if (attrs != null) {
            try {
                String hint = array.getString(R.styleable.headingtext_field_hint);
                String name = array.getString(R.styleable.headingtext_field_name);
                String text = array.getString(R.styleable.headingtext_field_text);
                Boolean isFocuable = array.getBoolean(R.styleable.headingtext_field_focus, true);
                if (!isFocuable) {
                    binding.edtTitle.setClickable(false);
                    binding.edtTitle.setFocusable(false);
                    binding.edtTitle.setFocusableInTouchMode(false);
                    binding.edtTitle.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ((View) v.getParent()).performClick();
                        }
                    });
//                    binding.edtTitle.setEnabled(false);
                } else {
                    binding.edtTitle.setClickable(true);
                    binding.edtTitle.setFocusable(true);
                    binding.edtTitle.setFocusableInTouchMode(true);
                }
                binding.edtTitle.setHint(hint);
                binding.tvTitle.setText(name);
                binding.edtTitle.setText(text);
            } finally {
                array.recycle();
            }
        }
    }

这就是我想使用它的方式:

        <packagename.customView.HeadingEditText
            android:id="@+id/edt_country"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="@string/mobile_or_wechat"
            android:onClick="@{()->frag.onCountryClick(countryList)}"
            app:field_focus="false"
            app:field_hint="@string/country"
            app:field_name="@string/country"
            app:field_text='@{basicinfo.country_name!=null?basicinfo.country_name:""}'
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/edt_address" />

但它仍然无法正常工作所以我认为数据绑定(bind)和点击我的自定义 View 存在问题。

最佳答案

程序化

//disable click action
editText.setFocusable(false);
editText.setEnabled(false);


//enable click action
editText.setFocusable(true);
editText.setEnabled(true);

xml:

android:focusable="false"
android:enabled="false"

关于android - 在自定义 View 中禁用 EditText OnTouchListener 或 OnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49398472/

相关文章:

安卓 Assets 工作室 : Creating icons with an image does not work

Android Play 上传失败,错误代码 : The maximum SDK version cannot be lower than the minimum SDK version

android - 如何创建如图所示的圆形编辑文本

android - Vimeo SDK 是否可用于安卓?

android - 如何在 Android 中升级 SQLite 数据库?

android - 无法让 EditText 获得焦点和弹出键盘

android - 清除 TextWatcher 的 beforeTextChanged() 中的 EditText 数据

android - 扩展 RecyclerView : Error Inflating Class com. example.MyRecyclerView

android - 在 android 中像简单形状(正方形和圆形)的设计者一样开发 View

android - 自定义 View 上的 findViewById 抛出 null