EditText一些属性

EditText一些属性

1.去掉下边框:android:background=”@null”

2.改变光标的颜色:android:textCursorDrawable=”@null”(光标颜色和字体颜色一致)可以在shap文件自定义光标颜色,例如:(<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:width="2dp" />
<solid android:color="#ce1717" />
</shape>)定义一个深红色色光标

2.始终将光标定位到最后一行:editText.setSelection(int index)

3.将输入框内容从左上角开始:android:gravity=”left|top”

editText文字内容监听,将字数控制在200字以内

private static final int TEXT_TOTAL = 200;//文字总数
    editText.addTextChangedListener(this);//添加监听,并实现以下三个方法
        /**
         * This method is called to notify you that, within <code>s</code>,
         * the <code>count</code> characters beginning at <code>start</code>
         * are about to be replaced by new text with length <code>after</code>.
         * It is an error to attempt to make changes to <code>s</code> from
         * this callback.
         *
         * @param s
         * @param start
         * @param count
         * @param after
         */
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        /**
         * This method is called to notify you that, within <code>s</code>,
         * the <code>count</code> characters beginning at <code>start</code>
         * have just replaced old text that had length <code>before</code>.
         * It is an error to attempt to make changes to <code>s</code> from
         * this callback.
         *
         * @param s
         * @param start
         * @param before
         * @param count
         */
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() > TEXT_TOTAL){
                Toast.makeText(mContext, "最多输入"+TEXT_TOTAL+"个字", Toast.LENGTH_SHORT).show();
                //当字数超过设定的字数时,截取前TEXT_TOTAL个字
                editText.setText(s.subSequence(0, TEXT_TOTAL));
            }else {
                //tv_length是显示当前输入的字数,格式为: 100/200
                tv_length.setText(s.length() + "/"+TEXT_TOTAL);
            }
        }

        /**
         * This method is called to notify you that, somewhere within
         * <code>s</code>, the text has been changed.
         * It is legitimate to make further changes to <code>s</code> from
         * this callback, but be careful not to get yourself into an infinite
         * loop, because any changes you make will cause this method to be
         * called again recursively.
         * (You are not told where the change took place because other
         * afterTextChanged() methods may already have made other changes
         * and invalidated the offsets.  But if you need to know here,
         * to mark your place and then look up from here where the span
         * ended up.
         *
         * @param s
         */
        @Override
        public void afterTextChanged(Editable s) {
            if (s.length() > TEXT_TOTAL){
                editText.setSelection(TEXT_TOTAL);
            }else{
                editText.setSelection(s.length());
            }

        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值