【问题标题】:EditText with cross(x) button at end of itEditText 末尾带有 cross(x) 按钮
【发布时间】:2014-01-28 16:24:37
【问题描述】:

有什么方法可以像 iPhone 一样在 android Editbox 中添加 x-graphics 因此,通过单击该 x 图形,它可以清除编辑框中的所有值

有什么方法可以听我触摸编辑文本的特定部分的天气 谢谢

【问题讨论】:

    标签: android editbox


    【解决方案1】:

    是的,有办法实现这一点。

    定义一个像这样的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 之上。我们将其右边缘设置为等于 EditTexts 的右边缘,以使其出现在右侧。

    现在您必须使用 if 覆盖方法为您的 ImageButton 分配一个 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("");
            }
    });
    

    }

    现在我们简单地告诉我们的 ImageViews OnClickListener 在单击时重置我们的 EditTexts 文本。就那么简单。 ;)

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

    【讨论】:

      【解决方案2】:

      你可以下载它,它就像 iPhone 一样工作

      https://github.com/GhOsTTT/editTextXbutton

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-19
        • 1970-01-01
        相关资源
        最近更新 更多