是否有像EditText这样包含交叉按钮的小部件,或者是否有自动创建EditText的属性?我想用十字按钮删除任何写在EditText中的文本。


当前回答

Android的支持库中有一个SearchView类就是这样做的。(虽然不是从EditText派生的,所以必须使用一个SearchView。OnQueryTextListener代替TextWatcher)

在XML中像这样使用:

  <android.support.v7.widget.SearchView
            android:id="@+id/searchView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:iconifiedByDefault="false"
            android:queryHint="@string/SearchHint"
            app:iconifiedByDefault="false"
            app:queryHint="@string/SearchHint" />

其他回答

Android的支持库中有一个SearchView类就是这样做的。(虽然不是从EditText派生的,所以必须使用一个SearchView。OnQueryTextListener代替TextWatcher)

在XML中像这样使用:

  <android.support.v7.widget.SearchView
            android:id="@+id/searchView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:iconifiedByDefault="false"
            android:queryHint="@string/SearchHint"
            app:iconifiedByDefault="false"
            app:queryHint="@string/SearchHint" />
Drawable x = getResources().getDrawable(R.drawable.x);
x.setBounds(0, 0, x.getIntrinsicWidth(), x.getIntrinsicHeight());
mEditText.setCompoundDrawables(null, null, x, null);

式中x为:

如果你碰巧使用DroidParts,我刚刚添加了clearleedittext。

下面是自定义背景和清除图标设置为abs__ic_clear_holo_light的效果:

这是一个kotlin溶液。把这个助手方法放在某个kotlin文件中

fun EditText.setupClearButtonWithAction() {

    addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(editable: Editable?) {
            val clearIcon = if (editable?.isNotEmpty() == true) R.drawable.ic_clear else 0
            setCompoundDrawablesWithIntrinsicBounds(0, 0, clearIcon, 0)
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit
    })

    setOnTouchListener(View.OnTouchListener { _, event ->
        if (event.action == MotionEvent.ACTION_UP) {
            if (event.rawX >= (this.right - this.compoundPaddingRight)) {
                this.setText("")
                return@OnTouchListener true
            }
        }
        return@OnTouchListener false
    })
}

然后像下面那样在onCreate方法中使用它,你应该很好

yourEditText.setupClearButtonWithAction()

顺便说一句,你必须添加R.drawable。Ic_clear或清除图标。这个来自谷歌- https://fonts.google.com/icons?selected=Material%20Icons%20Outlined%3Aclear%3A

Android材质设计组件2020解决方案:

添加材质组件到你的gradle设置:

从这里查找最新版本:https://maven.google.com/

implementation 'com.google.android.material:material:1.3.0'

或者如果你还没有更新到使用AndroidX库,你可以这样添加:

implementation 'com.android.support:design:28.0.0'

Then

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/hint_text"
    app:endIconMode="clear_text">

  <com.google.android.material.textfield.TextInputEditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

注意:app:endIconMode="clear_text"

正如这里所讨论的材料设计文档