1、代码
name_tv = (EditText) findViewById( R.id.name_tv );
name_tv.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String string = s.toString().trim();
//限定输入框最多输入18个字节
int inputLength = string.length();
int byteLength = string.getBytes().length;
if (byteLength >= 18) {
name_tv.setFilters(new InputFilter[]{new InputFilter.LengthFilter(inputLength)});
} else {
name_tv.setFilters(new InputFilter[]{new InputFilter.LengthFilter(18)});
}
name_tv.setSelection(name_tv.getText().toString().length());
}
});