forked from simtice/AndroidCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedittext字数限制.java
executable file
·71 lines (58 loc) · 2.7 KB
/
edittext字数限制.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
private EditText mEditText ;
mEditText = (EditText) findViewById(R.id.mEditText );
/** 限制字数 */
mEditText .addTextChangedListener(new TextWatcher() {
private CharSequence temp;
private int selectionStart ;
private int selectionEnd ;
@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) {
selectionStart = mEditText .getSelectionStart();
selectionEnd = mEditText .getSelectionEnd();
Log.d(TAG,""+selectionStart);
if (temp.length() > 8) {
Toast.makeText(MAUpdateAty.this, "字数不能超过8个", Toast.LENGTH_SHORT).show();
s.delete(selectionStart-1, selectionEnd);
int tempSelection = selectionStart;
mEditText .setText(s);
mEditText .setSelection(tempSelection);
}
Log.d(TAG," "+selectionEnd);
}
});
private TextWatcher mTextWatcher = new TextWatcher(){
Toast mToast = null;
public void beforeTextChanged(CharSequence s, int start,
int count,int after) {
}
public void onTextChanged(CharSequence s, int start,
int before,int count) {
}
public void afterTextChanged(Editable s) {
int nSelStart = 0;
int nSelEnd = 0;
boolean nOverMaxLength = false;
nSelStart = mEditText.getSelectionStart();
nSelEnd = mEditText.getSelectionEnd();
nOverMaxLength = (s.length() > Constants.MAX_TEXT_INPUT_LENGTH) ? true : false;
if(nOverMaxLength){
if(null == mToast){
mToast = Toast.makeText(mContext,
R.string.IDS_MSG_TEXT_OVER_MAXLENGTH,
Toast.LENGTH_SHORT);
}
mToast.show();
s.delete(nSelStart - 1, nSelEnd);
mEditText.setTextKeepState(s);//请读者注意这一行,保持光标原先的位置,而 mEditText.setText(s)会让光标跑到最前面,就算是再加mEditText.setSelection(nSelStart) 也不起作用
}
}
};