限制EditText 输入的字节数

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());
}
});
上一篇:POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】


下一篇:POJ 2594 Treasure Exploration (可相交最小路径覆盖)