Android EditText 监听字数变化



1.   edit_title.addTextChangedListener(mTextWatcher);



   //监听输入的直播标题的字数2. TextWatcher mTextWatcher = new TextWatcher() { private CharSequence temp; private int editStart; private int editEnd; @Override public void beforeTextChanged(CharSequence s, int arg1, int arg2, int arg3) { temp = s; } @Override public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) { if (s.length()>0){ te_wancheng.setVisibility(View.VISIBLE); te_num.setText(s.length() + "/20"); }else { te_num.setText("0/20"); te_wancheng.setVisibility(View.GONE); } } @Override public void afterTextChanged(Editable s) { editStart = edit_title.getSelectionStart(); editEnd = edit_title.getSelectionEnd(); if (temp.length() > 20) { s.delete(editStart - 1, editEnd); int tempSelection = editStart; edit_title.setText(s); edit_title.setSelection(tempSelection); } }};
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页