由于editText变得集中,drawable没有被点击

时间:2019-06-23 13:00:56

标签: android android-edittext android-toolbar

我实现了一个自定义工具栏,在该自定义工具栏中,我实现了一个自定义editText来清除editText中的数据。因此,为了使drawable正常工作,我实现了setDrawableClickListener,但它的工作原理是,当我第一次单击drawable时,editText进入文本选择模式,而drawable单击不起作用,但是下次单击时再单击drawable单击并清除文本。

因此,我需要在首次实现可绘制点击工作方面的帮助。

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

enter image description here

所以这一次,当我单击十字时,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>

1 个答案:

答案 0 :(得分:0)

您可以简单地执行以下操作:在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和EditTextOnTouchListener内部的主体。由于此方法适用于触摸监听器,因此只要触摸到触摸监听器,就会立即调用该监听器并采取适当的措施