文本编辑样式!
2016-03-22 10:17
555 查看
编辑框4个字符串一个符号的样式简易设置:在xml文件中设置:
<EditText android:id="@+id/et_code"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:inputType="number" //文本内容只能为数字
android:digits="0123456789-" //可输入的文本
android:maxLength="19"/> //文本的最大长度
然后在Activity的oncreate方法中找到该控件并且添加它的文本监听事件:
et_code = (EditText) findViewById(R.id.et_code);
et_code.addTextChangedListener(EditChangedListener);
创建文本监听:
/**文本监听处理*/
private TextWatcher EditChangedListener=new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void afterTextChanged(Editable s) {
//得到文本内容String str = et_code.getText().toString();
//如果文本不为退格状态并且长度为5的倍数不包含字符if (str.length() > 4 && str.length() % 5 == 0 && //“-”为自己想插入的字符
!(str.substring(str.length() - 4, str.length()).contains("-"))) {
//文本中插入字符s.insert(str.length() - 1, "-");
}}};
以下为效果图:
相关文章推荐
- css3毛玻璃模糊效果
- 基本图片与文字布局(浮动,包含块)
- 什么是CSS hack
- table css 控制
- DIV+CSS中标签dl dt dd常用的用法
- CSS学习(十九)-CSS伸缩布局盒
- CSS hack
- CSS-float
- css实现网页布局随滚轮变化响应移动
- 浅谈CSS布局
- CSS3(七)——布局样式
- CSS3(六)——变形与动画
- ActionMode使用及样式配置
- css的学习
- 使用css Flexbox实现垂直居中
- ActionBar样式
- 博客园样式漫谈——从0到1改变你的博客风格!
- CSS的width:100%和width:auto区别
- CSS Sprites
- padding-bottom和 margin-bottom