EditText控件类是TextView控件类的子类,具有TextView的所有特性。另外EditText对TextView进行了简单扩充。主要实现的功能是使得自身的文本内容可编辑。
EditText扩充后可以使用的方法都是针对EditText对象内部的Editable对象的操作。
代理操作类方法:选择字符方法
1. public void extendSelection(int index) : 这个方法设置EditText内容字符串从0到index字符被选中;
2.public void setSelection(int index): 作用同上;
3.public void selectAll(): 设置EditText内容全部被选中;
4.public void setSelection(int start, int stop):设置EditText内容从start到stop被选中;
注:这上面的方法实际上是对Editable对象的操作。
直接对Editable进行操作
1.Editable getText():这个方法可以获取EditText对象内部的Editable对象,然后对其进行直接操作。
Editable操作方法分类
Editable是一个接口,Editable.Factory内部类用来创建Editable对象,Editable.Factory创建的Editable对象实际上是SpannableStringBuilder对象。
简单字符串操作方法
1.append(Char char) : 向字符串末尾追加字符
2.append(CharSequence text): 向字符串末尾追加字符串;
3.append(CharSequence text,int start, int end):向字符串末尾追加字符串的子串,给出子串在字符串中的起止位置;
4.insert(int where, CharSequence text):在字符串给定的位置插入一个字符串,给定位置后面的字符顺延后移;
5.insert(int where, CharSequence text, int start ,int end):在字符串给定位置插入一个字符串的子串,给定位置后面的字符顺延后移;
6.replace(int start , int end, CharSequence text):把字符串中从start到end位置的子串替换为给定的字符串。
7.replace(int start, int end, CharSequence text, int st, int en):把字符串从start到end位置的子串替换为给定字符串text的子串,text子串在text中的位置由st,en给出;
8.delete(int start , int end):把指定的子串从字符串中删除;
9.clear():清空Editable对象中的字符;
10.charAt(int index):获取给定位置的字符;
11:length():获取Editable对象中字符串的长度;
12.subSequence(int start,int end):获取给定位置的子串;
13.getChars(int start , int end , char[] dest, int destOff):把从start到end子串插入到dest中,插入位置由destOff指定;
14.subString(int start, int end):获取给定位置的子串。
Spannable和Spanned操作:从Spannable到Editable/SpannableStringBuilder接口层结构如下:
Spannable和Spanned接口定义的操作下一篇文章详述。