EditText 字数限制输入

来源:互联网 发布:php 流媒体开发实战 编辑:程序博客网 时间:2024/05/07 15:29

private TextWatcher comment_limit_watcher = new TextWatcher() {
private CharSequence temp;  //要输入的字
        private int selectionStart ;  //输入的开始位置
        private int selectionEnd ; //输入的结束位置
        
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,int after) {}

        @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
        temp = s; 
}

@Override
public void afterTextChanged(Editable s) {
selectionStart = society_add_comment_content_et.getSelectionStart();  
             selectionEnd = society_add_comment_content_et.getSelectionEnd(); 
             int number = CommonConstant.COMMENT_MAX_LIMIT-temp.length();//还能输入多少字符
             society_add_comment_content_limit.setText(""+number);
             if (temp.length() > CommonConstant.COMMENT_MAX_LIMIT) {  
                 s.delete(selectionStart-1, selectionEnd);  
                 int tempSelection = selectionStart;  
                 society_add_comment_content_et.setText(s);  
                 society_add_comment_content_et.setSelection(tempSelection); 
             }  
}
};
0 0