如何在Android中将EditText提示创建为带有图像的文本 [英] How to create EditText hint as Text with image in android

查看:75
本文介绍了如何在Android中将EditText提示创建为带有图像的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Text&创建EditText提示/占位符Android中的图片.我这样写

How to create EditText hint/placeholder with Text & image in android. I write like this

<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不为空,则隐藏提示文本和图像

Here we can see image & text (hint) within edittext but image still visible even after user enters something in that field. My requirement is if there is no text in EditText show hint with image, if EditText is not empty hide hint text and image

推荐答案

您可以像在xml中一样设置drawableLeft并将addTextChangedListener添加到您的EditText中.键入时,将警告该侦听器,并且您可以在侦听器中检查textsize是否大于0.如果是,则只需以编程方式将xml中分配的drawableLeft设置为null.

What you can do is set an drawableLeft as you did in your xml and add an addTextChangedListener to your EditText. When typing occurs this listener will be warned and you can check in the listener if the textsize is bigger than 0. If so, then just set the drawableLeft assigned in the xml programmatically to 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);
    }
}

这篇关于如何在Android中将EditText提示创建为带有图像的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆