美文网首页帮助类
EditText中控制输入的字符长度

EditText中控制输入的字符长度

作者: 根艮哏艮根 | 来源:发表于2017-10-17 14:37 被阅读3次
     mEditText.addTextChangedListener(new TextWatcher() {
    
                private CharSequence temp;
                private int editStart;
                private int editEnd;
                private boolean isFrist = true;
    
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    temp = s;
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }
    
                @Override
                public void afterTextChanged(Editable s) {
                    editStart = edChangeData.getSelectionStart();
                    editEnd = edChangeData.getSelectionEnd();
                    if (temp.length() > 10) {
                        if (isFrist) {
                            Toast.makeText(activity,
                                    "你输入的字数已经超过了限制!", Toast.LENGTH_SHORT)
                                    .show();
                            isFrist = false;
                        }
                        s.delete(editStart - 1, editEnd);
                        int tempSelection = editStart;
                        edChangeData.setText(s);
                        edChangeData.setSelection(tempSelection);
                    }
    
                }
            });
    

    相关文章

      网友评论

        本文标题:EditText中控制输入的字符长度

        本文链接:https://www.haomeiwen.com/subject/djjduxtx.html