提起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监听。在改监听中检测输入字符是否合法。
关键代码如下:
   @Override  public 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();  }

更多相关文章

  1. android强制横屏息屏后重新打开时会先显示竖屏
  2. android源代码编译
  3. Android下如何计算要显示的字符串所占的宽度和高度
  4. android textview设置字体的行距和字间距
  5. Android(安卓)TextInputLayout 使用及其属性
  6. 朝花夕拾----新组件的学习和使用
  7. Android数字签名获取证书指纹(SHA1)
  8. Android:实现一种浮动选择菜单的效果
  9. Android(安卓)蓝牙耳机 语音输入与播放

随机推荐

  1. Android(安卓)MPAndroidChart--使用柱状
  2. GreenDao 3.0 简介、使用及踩坑
  3. Android(安卓)进阶解密读书笔记1
  4. Android(安卓)Studio 3.4 注解处理器不能
  5. Android(安卓)实现ListView 3D效果 - 1
  6. Unknown host ‘XXXX: nodename nor serv
  7. 一文详解Android(安卓)轻量级存储方案的
  8. Android(安卓)Secret Code
  9. Android的事件分发机制
  10. android 图形展示