EditText一些属性

来源:互联网 发布:畅捷通软件好用吗 编辑:程序博客网 时间:2024/05/16 06:13

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