EditText,末尾有十字(x)按钮

Lab*_*lan 2 android editbox

有没有办法在Android的Editbox中添加x-graphics,就像iPhone一样,通过点击该x图形,它可以清除Editbox中的所有值

替代文字

有没有办法听天气我触摸编辑文本的特定部分谢谢

Oct*_*ean 6

是的,有办法实现这一目标.

定义像这样的RelativeLayout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <EditText android:id="@+id/edittext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"/>
  <ImageButton android:id="@+id/clear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon"
    android:layout_alignRight="@id/edittext"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

现在发生了什么.ImageButton被绘制在EditText之上.我们将其右边缘设置为等于EditTexts的右边缘,以使其显示在右侧.

现在你必须为你的ImageButton分配一个带有if覆盖方法的OnCLickListener来将EditTexts文本设置为这样的空字符串.

EditText editText = null;
ImageButton clear = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.clear);

    editText = (EditText) findViewById(R.id.edittext);
    clear = (ImageButton) findViewById(R.id.clear);

    clear.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            editText.setText("");
        }
});
Run Code Online (Sandbox Code Playgroud)

}

现在,我们只需告诉我们的ImageViews OnClickListener在单击时重置EditTexts文本.就那么简单.;)

当然,我的例子不是使用非常美观的图像,但你可以自己微调图像.这个原则有效.