EditText descriptions = new EditText(this);
allEds.add(descriptions);
descriptions.setHint("Descriptions");
descriptions.setInputType(InputType.TYPE_CLASS_TEXT);
if("0".equals(partial_check)){ descriptions.setEnabled(true); }else{ descriptions.setEnabled(false); }
linearLayout.addView(descriptions);
答案 0 :(得分:1)
descriptions.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (s.length() > 0) {
if (s.charAt(s.length() - 1) == '&') {
descriptions.setText(s.subSequence(0, s.length() - 1));
descriptions.setSelection(s.length() - 1);
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
答案 1 :(得分:0)
为此使用edittext Ontextchanged 侦听器。检查当前文本是否包含&字符。如果是,请删除它并使用字符串重置edittext而不使用&字符。
此链接可让您了解具体用法: Counting Chars in EditText Changed Listener
答案 2 :(得分:0)
正如Gautham所说,使用OntextChangedListener来解决这类问题,它会在插入后检查每个字母并帮助你限制。类似的东西:
descriptions .addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
//restrict the word here...
}
});
有关ontextChanged和TextWatcher Tutorial以及Documentation的一些额外信息。
答案 3 :(得分:0)
我通过使用输入过滤器来做到这一点,您可以根据需要进行更改
InputFilter filter= new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
String checkMe = String.valueOf(source.charAt(i));
Pattern pattern = Pattern.compile("[+0123456789]");
Matcher matcher = pattern.matcher(checkMe);
boolean valid = matcher.matches();
if(!valid){
Log.d("", "invalid");
return "";
}
}
return null;
}
};
"[+0123456789]"
行Pattern pattern = Pattern.compile("[+0123456789]");
是什么将被接受。现在您的编辑文本设置此过滤器
yourEditTexy.setFilters(new InputFilter[]{filter});