【问题标题】:drawable not getting clicked due to editText getting focused由于editText获得焦点,drawable没有被点击
【发布时间】:2023-03-08 14:09:01
【问题描述】:

我实现了一个自定义工具栏,并且在该自定义工具栏中,我实现了一个自定义 editText 以清除 editText 中的数据。因此,为了让drawable工作,我实现了setDrawableClickListener并且它可以工作,但问题是当我第一次点击drawable时,我的editText进入文本选择模式并且drawable click不起作用但是下次我点击然后drawable click工作并且清除文本。

所以我在第一次实现可绘制点击工作时需要帮助。

自定义editText链接:Setting onClickListener for the Drawable right of an EditText

所以这次当我单击十字时,editText 蓝色光标变得可见,当我单击十字后,当这个蓝色十字消失时,editText 被清除。

<android.support.v7.widget.Toolbar android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:contentInsetStart="0dp"
        android:elevation="4dp"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/AlertDialog.AppCompat.Light"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <LinearLayout
            android:background="@color/colorPrimary"
            android:padding="2.5dp"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <com.example.mybrowser.customEditText
                android:drawableRight="@drawable/ic_clear_black_24dp"
                android:drawablePadding="5dp"      
                android:inputType="textUri"
                android:hint="Enter URL"
                android:background="@drawable/rectangle"
                android:id="@+id/urlEditText"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent" />
            <Button
                android:layout_margin="2dp"
                android:background="@drawable/ic_send_black_24dp"
                android:layout_width="50dp"
                android:layout_height="50dp"
                />
        </LinearLayout>
    </android.support.v7.widget.Toolbar>

【问题讨论】:

    标签: android android-edittext android-toolbar


    【解决方案1】:

    您可以简单地做的是,在您的editText 中使用 android:drawableRight = R.drawable.close 创建一个edittext

    <EditText
    .....
    android:drawableRight = R.drawable.close
    .....
    />
    

    然后将这些侦听器附加到它

    open class SimpleWatcher(val inputLayout: TextInputLayout,
                            val context: Context?,
                            val editText: EditText,val shouldShowCloseButton:Boolean = true): TextWatcher {
    override fun afterTextChanged(s: Editable?) {
    }
    
    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    }
    
    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
    
        if(s?.isNotEmpty() == true){
            if(shouldShowCloseButton) {
                context?.let {
       //This is the important one. Sets back the drawable after removing it when the text is empty
                    editText.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(it, R.drawable.ic_edit_text_close), null)
                }
            }
            inputLayout.error = null
        }else{
            if(shouldShowCloseButton) {
                editText.setCompoundDrawables(null, null, null, null)
            }
        }
    }
    

    }

     class EditTextOnTouchListener(val view: EditText): View.OnTouchListener{
    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        val DRAWABLE_RIGHT = 2
        if(event?.action == MotionEvent.ACTION_UP){
            if(view.compoundDrawables[DRAWABLE_RIGHT] != null) {
                if (event.rawX >= (view.right - 
      view.compoundDrawables[DRAWABLE_RIGHT].bounds.width())) {
      //                        Toast.makeText(context, "Close Clicked", 
      Toast.LENGTH_SHORT).show()
                    view.text = Editable.Factory.getInstance().newEditable("")
                    view.clearFocus()
                    return true
                }
            }
        }
        return false
    }
    

    }

    拿走必要的,忽略其余的。主要是onTextChanged里面的body和EditTextOnTouchListener。由于这适用于触摸侦听器,因此只要有触摸发生,就会调用侦听器并采取适当的操作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      • 1970-01-01
      相关资源
      最近更新 更多