您的位置:首页 > 移动开发 > Android开发

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)}); 

    } 



<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



 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: