Android:SELECTED时暂时清除EditText(未写入)

时间:2017-12-18 00:03:43

标签: android

是否有一种方法可以在SELECTED时“临时”清除EditText? 换句话说,我想实现这些步骤:

1)在开始时它会出现提示

2)一旦我选择了EditText,它就会显示为空,只有可见的闪电光标

3)没有点击'发送',所以如果没有确认书面文字,我点击活动中的其他地方

4)初始提示将再次出现

我尝试使用 android:hint ,但是当选择EditText时文本不会消失。 任何人都可以帮助我吗?

感谢。

1 个答案:

答案 0 :(得分:1)

当EditText聚焦时,您需要提示文本颜色透明,正常状态下需要灰色。在text_color_hint.xml中创建res/drawable文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:color="@android:color/transparent" />
    <item android:color="#808080" />
</selector>

然后将text_color_hint分配给android:textColorHint。您的EditText应如下所示:

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="HINT HERE"
        android:textColorHint="@drawable/text_color_hint"/>


要避免EditText自动聚焦,您可以:

1)在EditText的布局中添加android:focusable="true"android:focusableInTouchMode="true",此处我以LinearLayout为例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true" 
    android:focusableInTouchMode="true">

OR

2)创建一个虚拟的LinearLayout,它不占用空格,将上面的2个元素添加到它,然后将放在 EditText之前:

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:focusable="true"
    android:focusableInTouchMode="true"/>

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="HINT HERE"
        android:textColorHint="@drawable/text_color_hint"/>
相关问题