末尾带有十字 (x) 按钮的 EditText

发布于 2024-09-29 15:56:08 字数 167 浏览 4 评论 0原文

有没有办法像iPhone一样在android编辑框中添加x图形 这样,通过单击该 x 图形,它可以清除编辑框中的所有值

alt text

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

Is there any way to add the x-graphics in the android Editbox like the of iPhone
So that by clicking on that x graphic it can clear all the values in the Editbox

alt text

Is there any way to listen weather i touch a specific part of an edit text
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乞讨 2024-10-06 15:56:08

是的,有一种方法可以实现这一目标。

定义一个像这样的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>

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

现在,您必须为 ImageButton 分配一个 OnCLickListener ,并使用 if 重写方法将 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("");
        }
});

现在,

我们只是告诉 ImageViews OnClickListener 在单击时重置 EditTexts 文本。就这么简单。 ;)

当然,我的示例使用的图像不是很美观,但您可以自己微调图像。这个原理是有效的。

Yes there is a way to achieve this.

Define a RelativeLayout like this one.

<?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>

Now what happens. The ImageButton gets drawn on top of the EditText. We set its right edge to be equal to the right edge of the EditTexts in order to get it appear on the right side.

Now you have to assign your ImageButton an OnCLickListener with if overridden method to just set the EditTexts text to a empty string like that.

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("");
        }
});

}

Now here we simply tell our ImageViews OnClickListener to reset our EditTexts text upon a click. Simple as that. ;)

Of course my example uses not very aesthetic images but you can fine tune the images yourself. The principle works.

简单气质女生网名 2024-10-06 15:56:08

您可以下载它,它的工作方式与 iPhone

https://github.com/GhOsTTT/editTextXbutton

You can download it, it works like iPhone

https://github.com/GhOsTTT/editTextXbutton

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文