EditText的光标选择(android.text.Selection)&输入指定字符
2011-10-05 11:54
465 查看
提起Android的EditText的光标选择问题,可以通过android.text.Selection包提供的方法来实现,Android SDK提供了有关光标选择的多种方法,比如说getSelectionEnd、getSelectionStart、removeSelection、selectAll、setSelection,详细的参数声明如下
final static int getSelectionEnd(CharSequence text)
Return the offset of the selection edge or cursor, or -1 if there is no selection or cursor.
final static int getSelectionStart(CharSequence text)
Return the offset of the selection anchor or cursor, or -1 if there is no selection or cursor.
final static void removeSelection(Spannable text)
Remove the selection or cursor, if any, from the text.
final static void selectAll(Spannable text)
Select the entire text.
final static void setSelection(Spannable text, int index)
Move the cursor to offset index.
static void setSelection(Spannable text, int start, int stop)
Set the selection anchor to start and the selection edge to stop.
Android123提示大家,从上面的参数来看,可以发现Spannable类型,常规我们的EditText中的编辑中Editable直接实现Spannable接口,所以我们可以通过下面的方法来设置选择:
Editable ea= etEdit.getText(); //etEdit为EditText
Selection.setSelection(ea, ea.length()-1); // Android开发网提示这里ea的长度必须大于1。否则会有异常发生
http://www.android123.com.cn/kaifafaq/513.html
Android自由选择TextView的文字
http://chroya.iteye.com/blog/753634
自定义程序实现EditText只允许输入指定字符
http://www.eoeandroid.com/thread-66266-1-1.html
程序设计的很多地方都要用到一个小技术:指定文本框的输入类型。即限制只能输入某几类或某类字符,甚至是某几个字符。
Android本身已经做了很多设计,如限制长度,限制只能输入整数或数字。
有时候这些还是不够的。我们可以在程序中根据需要自己定制。
主要涉及:EditText.addTextChangedListener,EditText.removeTextChangedListener,EditText.setFilters。
方法:
对EditText添加自定义的TextChange监听。在改监听中检测输入字符是否合法。
关键代码如下:
Java代码
@Override
ublic void afterTextChanged(Editable s) {
String str = s.toString();
if (str.equals(tmp)) {
return;// 如果tmp==str则返回,因为这是我们设置的结果。否则会形成死循环。
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
if (digits.indexOf(str.charAt(i)) >= 0) {// 判断字符是否在可以输入的字符串中
sb.append(str.charAt(i));// 如果是,就添加到结果里,否则跳过
}
}
tmp = sb.toString();// 设置tmp,因为下面一句还会导致该事件被触发
editText.setText(tmp);// 设置结果
editText.invalidate();
/article/3747665.html
final static int getSelectionEnd(CharSequence text)
Return the offset of the selection edge or cursor, or -1 if there is no selection or cursor.
final static int getSelectionStart(CharSequence text)
Return the offset of the selection anchor or cursor, or -1 if there is no selection or cursor.
final static void removeSelection(Spannable text)
Remove the selection or cursor, if any, from the text.
final static void selectAll(Spannable text)
Select the entire text.
final static void setSelection(Spannable text, int index)
Move the cursor to offset index.
static void setSelection(Spannable text, int start, int stop)
Set the selection anchor to start and the selection edge to stop.
Android123提示大家,从上面的参数来看,可以发现Spannable类型,常规我们的EditText中的编辑中Editable直接实现Spannable接口,所以我们可以通过下面的方法来设置选择:
Editable ea= etEdit.getText(); //etEdit为EditText
Selection.setSelection(ea, ea.length()-1); // Android开发网提示这里ea的长度必须大于1。否则会有异常发生
http://www.android123.com.cn/kaifafaq/513.html
Android自由选择TextView的文字
http://chroya.iteye.com/blog/753634
自定义程序实现EditText只允许输入指定字符
http://www.eoeandroid.com/thread-66266-1-1.html
程序设计的很多地方都要用到一个小技术:指定文本框的输入类型。即限制只能输入某几类或某类字符,甚至是某几个字符。
Android本身已经做了很多设计,如限制长度,限制只能输入整数或数字。
有时候这些还是不够的。我们可以在程序中根据需要自己定制。
主要涉及:EditText.addTextChangedListener,EditText.removeTextChangedListener,EditText.setFilters。
方法:
对EditText添加自定义的TextChange监听。在改监听中检测输入字符是否合法。
关键代码如下:
Java代码
@Override
ublic void afterTextChanged(Editable s) {
String str = s.toString();
if (str.equals(tmp)) {
return;// 如果tmp==str则返回,因为这是我们设置的结果。否则会形成死循环。
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
if (digits.indexOf(str.charAt(i)) >= 0) {// 判断字符是否在可以输入的字符串中
sb.append(str.charAt(i));// 如果是,就添加到结果里,否则跳过
}
}
tmp = sb.toString();// 设置tmp,因为下面一句还会导致该事件被触发
editText.setText(tmp);// 设置结果
editText.invalidate();
/article/3747665.html
相关文章推荐
- EditText的光标选择(android.text.Selection)&输入指定字符
- android EditText 只允许输入指定字符
- 自定义程序实现Android EditText只允许输入指定字符
- android EditText 只允许输入指定字符
- android控件EditText限制指定字符输入
- android 中如何限制 EditText 最大输入字符数
- android EditText获取光标位置并安插字符删除字符
- Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
- 【EditText】Android验证密码文本框是否输入字符
- Android EditText禁止输入中文字符《1》
- Android之EditText只能输入数字或自定义字符
- iOS 控制textField只能输入指定字符
- adb shell input text 输入特殊字符 &
- Android统计当前输入字符数,限制edittext输入字符数
- Android TextView设置显示指定字符,超过部分显示...(省略号)
- 1怎样让输入的text文本填写的部分只添加数字、2怎样将输入的数字一次性全部删除 3怎样选择密码 让它不显示 4 怎样实现在没有return的情况下点击button的情况下就能够使键盘页面下落5 怎样实现点击指定的button来对使指定的text进行键盘弹出 6怎样改变button按钮的状态
- adb shell input text 输入特殊字符 &
- android 中如何限制 EditText 最大输入字符数
- android同时控制EditText输入字符个数和禁止特殊字符输入的方法
- Android EditText的输入监听,输入字符的动态获取