android EditText控制光标的位置

浏览:
字体:
发布时间:2013-12-22 23:48:28
来源:

利用自定义键盘,需要手动删除编辑框中的文本时,会根据光标的位置来删除字符。那么,如何来控制光标呢,android为我们提供了哪些方法,来处理光标呢?

这里提供几个自己写的方法,根据这些方法可以满足在光标任何位置删除字符的要求。

// 往文本框中添加内容	public void addString(String sequence) {		int index = getEditSelection();// 光标的位置		if (index < 0 || index >= getEditTextViewString().length()) {			dia_et_pwd.append(sequence);			Log.i(TAG, "str===" + str);		} else {			dia_et_pwd.getEditableText().insert(index, sequence);// 光标所在位置插入文字		}	}	// 获取光标当前位置	public int getEditSelection() {		return dia_et_pwd.getSelectionStart();	}	// 获取文本框的内容	public String getEditTextViewString() {		return dia_et_pwd.getText().toString();	}	// 清除文本框中的内容	public void clearText() {		dia_et_pwd.getText().clear();	}	// 删除指定位置的字符	public void deleteEditValue(int index) {		dia_et_pwd.getText().delete(index - 1, index);	}	// 设置光标位置	public void setEditSelectionLoc(int index) {		dia_et_pwd.setSelection(index);	}	// 判断是否是数字	public static boolean isNum(String str) {		return str.matches("([0-9]+)?)$");	}


>更多相关文章
24小时热门资讯
24小时回复排行
资讯 | QQ | 安全 | 编程 | 数据库 | 系统 | 网络 | 考试 | 站长 | 关于东联 | 安全雇佣 | 搞笑视频大全 | 微信学院 | 视频课程 |
关于我们 | 联系我们 | 广告服务 | 免责申明 | 作品发布 | 网站地图 | 官方微博 | 技术培训
Copyright © 2007 - 2024 Vm888.Com. All Rights Reserved
粤公网安备 44060402001498号 粤ICP备19097316号 请遵循相关法律法规
');})();