如何在中心的edittext中设置文本和图像作为提示? [英] how to set text and image as a hint in edittext in center?

查看:67
本文介绍了如何在中心的edittext中设置文本和图像作为提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像和文本设置为提示,例如在中心编辑文本

i want to set image and text as hint in Edit text in center like this

我的XML代码是

<EditText  android:layout_width="300dp" android:layout_height="40dp" 
android:id="@+id/ed_username" android:layout_marginBottom="152dp" 
android:hint="@string/username"
android:drawableLeft="@drawable/username_image" 
android:textColorHint="@color/skyblue" 
 android:background="@drawable/edittext_shape" 
android:layout_alignParentBottom="true" 
android:layout_centerHorizontal="true" 
android:ellipsize="start" android:gravity="center|center_horizontal"/>

请先帮助我,谢谢

推荐答案

您可以在edittext中将图像添加为drawableleft,并在获得焦点时将其删除.

You can add an image in edittext as drawableleft and remove it while getting focus.

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Username"
android:drawableLeft="@drawable/yourimage" />

在获得焦点时将其删除

final EditText et=(EditText) findViewById(R.id.editText1);
et.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View arg0, boolean gotfocus) {
        // TODO Auto-generated method stub
        if(gotfocus){
    et.setCompoundDrawables(null, null, null, null);
}
        else if(!gotfocus){
            if(et.getText().length()==0)
            et.setCompoundDrawablesWithIntrinsicBounds(R.drawable.yourimage, 0, 0, 0);
        }


    }
});

这篇关于如何在中心的edittext中设置文本和图像作为提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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