Android-02 如何限制 EditText 最大输入字符数
2015-11-04 15:30
603 查看
方法一:
在 xml 文件中设置文本编辑框属性作字符数限制
如:android:maxLength="10" 即限制最大输入字符个数为10
方法二:
在代码中使用InputFilter 进行过滤
//editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 即限定最大输入字符数为20
方法二:
在代码中使用InputFilter 进行过滤
//editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 即限定最大输入字符数为20
publicclass TextEditActivity extends Activity
{
/** Called when the activity is first created.
*/
@Override
publicvoid onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText editText = (EditText)findViewById(R.id.entry);
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
}
}
在 xml 文件中设置文本编辑框属性作字符数限制
如:android:maxLength="10" 即限制最大输入字符个数为10
方法二:
在代码中使用InputFilter 进行过滤
//editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 即限定最大输入字符数为20
方法二:
在代码中使用InputFilter 进行过滤
//editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 即限定最大输入字符数为20
publicclass TextEditActivity extends Activity
{
/** Called when the activity is first created.
*/
@Override
publicvoid onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText editText = (EditText)findViewById(R.id.entry);
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
}
}
<p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(73, 73, 73); font-size: 14px; line-height: 22.4px; background-color: rgb(244, 237, 227);">方法三:</p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(73, 73, 73); font-size: 14px; line-height: 22.4px; background-color: rgb(244, 237, 227);">利用 TextWatcher 进行监听</p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(73, 73, 73); font-size: 14px; line-height: 22.4px; background-color: rgb(244, 237, 227);"> </p><div class="dp-highlighter bg_java" style="padding: 0px; font-family: Consolas, 'Courier New', Courier, mono, serif; overflow: auto; word-break: break-word; color: rgb(73, 73, 73); margin: 18px 0px !important; background-color: rgb(244, 237, 227);"><ol class="dp-j" style="padding-left: 50px; border: none; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; padding-top: 0px !important; padding-right: 0px !important; padding-bottom: 0px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">package</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> cie.textEdit; </span></span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> android.text.Editable; </span></span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> android.text.Selection; </span></span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> android.text.TextWatcher; </span></span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> android.widget.EditText; </span></span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">/*</span></span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">* 监听输入内容是否超出最大长度,并设置光标位置</span></span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">* */</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">public</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> MaxLengthWatcher </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">implements</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> TextWatcher { </span></span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">private</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> maxLen = </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">; </span></span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">private</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> EditText editText = </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">null</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">; </span></span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> MaxLengthWatcher(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> maxLen, EditText editText) { </span></span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">this</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">.maxLen = maxLen; </span></span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">this</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">.editText = editText; </span></span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> } </span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">public</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> afterTextChanged(Editable arg0) { </span></span></li><li class="alt" style="list-style: decimal outside; border-style: none none none sol fb64 id; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// TODO Auto-generated method stub</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> } </span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">public</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> beforeTextChanged(CharSequence arg0, </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> arg1, </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> arg2, </span></span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> arg3) { </span></span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// TODO Auto-generated method stub</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> } </span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">public</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> onTextChanged(CharSequence arg0, </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> arg1, </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> arg2, </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> arg3) { </span></span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// TODO Auto-generated method stub</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> Editable editable = editText.getText(); </span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> len = editable.length(); </span></span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">if</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(len > maxLen) </span></span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> { </span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> selEndIndex = Selection.getSelectionEnd(editable); </span></span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> String str = editable.toString(); </span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//截取新字符串</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> String newStr = str.substring(<span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,maxLen); </span></span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> editText.setText(newStr); </span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> editable = editText.getText(); </span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//新字符串的长度</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> newLen = editable.length(); </span></span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//旧光标位置超过字符串长度</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">if</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(selEndIndex > newLen) </span></span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> { </span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> selEndIndex = editable.length(); </span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> } </span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//设置新光标所在的位置</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> Selection.setSelection(editable, selEndIndex); </span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> } </span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> } </span></li><li class="alt" style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li style="list-style: decimal outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">} </span></li></ol></div><pre name="code" class="java" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(73, 73, 73); font-size: 14px; line-height: 22.4px; background-color: rgb(244, 237, 227);">文章来源:http://www.cnblogs.com/jason-star/archive/2013/01/01/2841596.html
相关文章推荐
- android开发步步为营之81:android图片处理技术之一(截取与缩放)
- Android Studio系列教程三--快捷键
- Android Studio系列教程二--基本设置与运行
- Android开发:Fragment不同操作的生命周期
- android ExpandableListView 四级目录结构
- 【Android学习笔记】Android概述与开发环境搭建
- Android源码编译
- Android:Layout_weight的深刻理解
- Android环境配置
- Android Studio 快捷键
- Android适配屏幕
- Android编程解析Json格式数据的方法
- Android MVC模式实例(三)
- Android客户端性能测试常见指标及测试方法--转载
- Android merge标签讲解与实例
- Android MVC模式实例(二)
- android studio首次运行出错
- Android客户端性能测试小结--转载
- Android动画的优化
- Android ViewTreeObserver