EditText的setCompoundDrawables用法

在EditText的左侧,添加上用户头像,或者密码的标识.

1.布局文件

 <EditText
        android:id="@+id/username"
        android:layout_width="fill_parent"
        android:layout_height="45dip"
        android:layout_marginTop="30dip"
        android:background="@drawable/txtborder"
        android:ellipsize="end"
        android:gravity="center_vertical"
        android:hint="请输入用户名"
        android:maxLength="30"
        android:paddingRight="2dp"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColorHighlight="#7e4d84" />


2.在代码中去设置setComnpoundDrawables

mIconPerson=getResources().getDrawable(R.drawable.txt_person_icon);
		mIconPerson.setBounds(5, 1, 60, 50);

		username=(EditText)findViewById(R.id.username);
		username.setCompoundDrawables(mIconPerson, null, null, null);


3.输入框EditText的边框设置

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <!--  <solid android:color="#EEEEEE" /> 这里是背景的颜色-->
   
   <solid android:color="#ffffff" />
   <!-- 这里是边框的颜色和宽度 -->
    <stroke android:width="8dp" android:color="#ffffff" />

    <corners android:radius="2dp" />

    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>


这样就是实现了在输入框内的左侧的图片.


思路二:

用LinearLayout 包裹一个imageView和一个EditText,这样也能实现上述效果.

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="30dp"
        android:background="@drawable/txtborder"
        android:gravity="center|left"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/txt_person_icon" />

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="45dip"
            android:layout_gravity="center"
            android:ellipsize="end"
            android:paddingRight="2dp"
            android:password="true"
            android:background = "@null"
            android:singleLine="true" />
    </LinearLayout>




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以在 EditText 的布局文件中添加以下属性来显示删除叉: ```xml <EditText ... android:drawableRight="@drawable/ic_clear" android:drawableEnd="@drawable/ic_clear" android:drawablePadding="8dp" /> ``` 其中 `@drawable/ic_clear` 是一个删除图标的资源文件,在这里你可以使用系统自带的 `android.R.drawable.ic_menu_close_clear_cancel` 或者自己制作一个图标。`android:drawablePadding` 属性用于设置删除图标与 EditText 文字间的间距,可以根据需要进行调整。 然后在 Activity 或者 Fragment 中对 EditText 添加 TextWatcher,监听文本变化事件,当用户输入内容后,删除图标就会自动显示: ```java EditText editText = findViewById(R.id.editText); editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (s.length() > 0) { editText.setCompoundDrawables(null, null, getResources().getDrawable(R.drawable.ic_clear), null); } else { editText.setCompoundDrawables(null, null, null, null); } } @Override public void afterTextChanged(Editable s) {} }); ``` 在 `onTextChanged` 方法中,通过判断文本长度来决定是否显示删除图标,如果文本长度大于 0,则显示删除图标,否则隐藏删除图标。调用 `editText.setCompoundDrawables` 方法来设置 EditText 的左、上、右、下四个方向的 drawable,这里只需要设置右边的 drawable 即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值