如何在Edittext中强制使用特定格式。 例如:
用户输入为:123456789AB 格式应为:123.4567.89AB 最好的是,格式的转换是即时的。 第二个解决方案是,在backround中转换字符串而不在Edittext中显示。
ps:Edittext是在AlertBuilder中动态创建的
答案 0 :(得分:0)
将侦听器(textWatcher)添加到EditText
yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// you can call or do what you want with your EditText here
yourEditText. ...
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
你可以阻止它进入它。您还可以为其添加过滤器。
如果要在EditText中过滤输入字符,则需要使用InputFilter。这是一个例子。 //仅允许字母或数字
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++) {
if (!Character.isLetterOrDigit(source.charAt(i))) {
return "";
}
}
return null;
}
};
EditText text = (EditText)findViewById(R.id.edittext1);
text.setFilters(new InputFilter[]{filter});
答案 1 :(得分:0)
希望这对你有用
yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(count%3==0)
((EditText)findViewById(R.id.yourEditText).setText(((EditText)findViewById(R.id.yourEditText).getText().toString()+"."));
}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
类似的,你可以按照你的要求继续