Android EditText 自定义

本文介绍如何在Android中自定义EditText光标样式,并通过添加文本更改监听器来监控输入文本的长度变化,实现不同状态下的文本显示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、提示内容颜色
android:textColorHint="@color/text_gray"


2、内容开始位置于左上角

android:textAlignment="textStart"
android:gravity="start"

3、自定义光标颜色和粗细,在res/drawable下新建cursor_sytle 
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <size android:width="1.5dp"/>
    <solid android:color="@color/blue"/>
</shape>

3.1 设置EditText的属性:textCursorDrawable

 android:textCursorDrawable="@drawable/cursor_style"

3.2 设置EditText的属性:textCursorDrawable="@null"  则光标的颜色与文本的颜色一致


4、监控EditText是否有输入,且获取输入长度

mEditText.addTextChangedListener(textWatcher);

TextWatcher textWatcher = new TextWatcher() {

       // TextView textView = (TextView)findViewById(R.id.float_window_add);

        @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){

                mAddTextView.setTextColor(getResources().getColor(R.color.blue));   //EditText有输入时,改变textView的颜色
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.length() == 0){
                mAddTextView.setTextColor(getResources().getColor(R.color.text_gray));  //输入完毕,若字符长度为空时,为没输入
            }
        }
    };


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值