如何在 android 中使用文本和图像创建 EditText 提示/占位符。我这样写
<EditText
android:id="@+id/name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:lines="1"
................................
...............................
android:drwableLeft="@drawable/image1"
android:hint="enter name here"/>
在这里,我们可以在编辑文本中看到图像和文本(提示),但即使用户在该字段中输入某些内容后,图像仍然可见。我的要求是如果EditText中没有文本显示带有图像的提示,如果EditText不为空则隐藏提示文本和图像
你能做的就是设置一个drawableLeft
正如您在 xml 中所做的那样并添加addTextChangedListener
到你的编辑文本。当输入发生时,这个侦听器将收到警告,您可以检查侦听器中的文本大小是否大于 0。如果是这样,则只需设置drawableLeft
以编程方式在 xml 中分配给null
.
@Override
public void afterTextChanged(Editable s) {
if(s.toString().length() > 0) {
editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
} else {
//Assign your image again to the view, otherwise it will always be gone even if the text is 0 again.
editText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.image, 0, 0, 0);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)