首页

> 期刊发表知识库

首页 期刊发表知识库 问题

android编辑框属性

发布时间:

android编辑框属性

方法一:在 xml 文件中设置文本编辑框属性作字符数限制如:android:maxLength="10" 即限制最大输入字符个数为10方法二:在代码中使用InputFilter 进行过滤//editTsetFilters(new InputFilter[]{new InputFLengthFilter(20)}); 即限定最大输入字符数为20[java] view plain copypublic class TextEditActivity extends Activity { /** Called when the activity is first */ @Override public void onCreate(Bundle savedInstanceState) { Create(savedInstanceState); setContentView(Rain); EditText editText = (EditText)findViewById(Ry); editTsetFilters(new InputFilter[]{new InputFLengthFilter(20)}); } } 方法三:利用 TextWatcher 进行监听[java] view plain copypackage xtEdit; import Editable; import Selection; import TextWatcher; import EditText; /* * 监听输入内容是否超出最大长度,并设置光标位置 * */ public class MaxLengthWatcher implements TextWatcher { private int maxLen = 0; private EditText editText = null; public MaxLengthWatcher(int maxLen, EditText editText) { axLen = maxLen; ditText = editText; } public void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub } public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub Editable editable = editTText(); int len = length(); if(len > maxLen) { int selEndIndex = SSelectionEnd(editable); String str = String(); //截取新字符串 String newStr = substring(0,maxLen); editTsetText(newStr); editable = editTText(); //新字符串的长度 int newLen = length(); //旧光标位置超过字符串长度 if(selEndIndex > newLen) { selEndIndex = length(); } //设置新光标所在的位置 SsetSelection(editable, selEndIndex); } } } 对应的 activity 部分的调用为:[java] view plain copypackage xtEdit; import Activity; import Bundle; import InputFilter; import EditText; public class TextEditActivity extends Activity { /** Called when the activity is first */ @Override public void onCreate(Bundle savedInstanceState) { Create(savedInstanceState); setContentView(Rain); EditText editText = (EditText) findViewById(Ry); editTaddTextChangedListener(new MaxLengthWatcher(10, editText)); } } 限制输入字符数为10个xml 文件[html] view plain copy

相关百科

热门百科

首页
发表服务