【问题标题】:Disable EditText OnTouchListener or OnClickListener in custom view在自定义视图中禁用 EditText OnTouchListener 或 OnClickListener
【发布时间】:2018-03-21 04:57:23
【问题描述】:

我有一个自定义的View,其中包含一个LinearLayout,依次包含一个TextView 和一个EditText。当我尝试点击我的自定义视图时,如果我按下 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 时,我希望父视图(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" />

但它仍然无法正常工作所以我认为数据绑定问题并单击我的自定义视图。

【问题讨论】:

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


【解决方案1】:

以编程方式

//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"

【讨论】:

  • 我会按 Edittext 我不会点击 LinearLayout 意味着如果你点击 edittext 需要执行的线性布局监听器?
  • 我已经编写了自定义视图的点击活动。如果我按下edittext部分,那么我将不会收到我的活动的点击事件。
  • 是的,在少数情况下(来自 attrs,如果我会变得不可聚焦,那么我不希望 edittext 可编辑)。但对于其他情况,我希望它是可编辑的。
  • 我是否需要获取edittext的父级并在单击或触摸时执行单击它?
  • 您如何决定edittext 何时可编辑或不可编辑?使用任何真假数组?
【解决方案2】:

EditText 试试这个:

 android:focusable="true"
 android:focusableInTouchMode="true" 

以编程方式:

editText.setFocusable(false);

===============编辑================

 if (!isFocuable) {
                    binding.edtTitle.setClickable(false);
                    binding.edtTitle.setFocusable(false);
                    binding.edtTitle.setFocusableInTouchMode(false);
                    binding.edtTitle.setOnTouchListener(null);
                    binding.edtTitle.setOnClickListener(null);
//                    binding.edtTitle.setEnabled(false);
                } else {

                    binding.edtTitle.setClickable(true);
                    binding.edtTitle.setFocusable(true);
                    binding.edtTitle.setFocusableInTouchMode(true);
                    binding.edtTitle.setOnTouchListener(this);
                    binding.edtTitle.setOnClickListener(this);
}

===============编辑================

在编辑文本中添加

android:duplicateParentState="true"

如果这不起作用,那么也试试这一行

android:duplicateParentState="true"
android:clickable="false"

【讨论】:

  • 实际上这是我的自定义视图,所以在少数情况下我想将edittext作为edittext,但在少数情况下我希望它作为textview..所以如果我将它作为静态添加到xml中,那么我将工作相同为所有人。
  • @HungryEnglish 检查已编辑的答案,在您要禁用的条件下使用它。
  • @HungryEnglish 在上面的代码中还添加了 else 并且在 else 中添加了所有的 true
  • 感谢您的回复,@Vishva Dave 我已经为它设置了一个属性,谁可以将布尔值传递给我,因为它是可编辑的。所以当这个视图不可编辑时,我设置了 Click , focus , 触摸一切为空。但不工作..
  • 我添加了自定义视图的活动 xml。
【解决方案3】:

这是一个变通方法。

// Inside HeadingEditText.kt
binding.edtTitle.setOnClickListener {
 this@HeadingEditText.performClick()
}

在您的活动中设置OnClickListener

edt_country.edtTitle.setOnClickListener {
  // foo action
}

【讨论】:

    【解决方案4】:

    不是一个好的解决方案

    仅可在使用 FrameLayout 的自定义视图中使用

    在包含EditText父级(ViewGroup)上,像这样覆盖onInterceptTouchEvent 方法:

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        return true
    }
    

    我在FrameLayout 中使用EditText覆盖 我的FrameLayout 中的方法,并且所有这些方法都在回收器列表项(xml)中使用......并且工作正常点击监听器回收站根项目视图...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多