Eli*_*ior 5 android android-edittext
在android中,我想要一个限制为255字节的编辑文本,所以当用户试图超过这个限制时,它不会让他写.
我看到的所有过滤器都使用字符限制,即使在xml布局中也是如此.
那么如何设置过滤器到edittext以限制为255个字节?
一种解决方案是。
根据您设置的阈值(以字节为单位)对 EditText 执行操作。
final int threshold = 255;
EditText editText = new EditText(getActivity());
editText.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) {
int i = s.toString().getBytes().length;
if(i < threshold){
//Action needed
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
Run Code Online (Sandbox Code Playgroud)您需要将此示例应用到您自己的解决方案中。
归档时间: |
|
查看次数: |
1317 次 |
最近记录: |