【发布时间】:2017-03-23 01:32:22
【问题描述】:
发送前如何验证在edittext中输入的USSD代码
这里我放了一些代码,请帮助我。
ussd_edittext.addTextChangedListener(new TextWatcher()
{
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
ussd_edittext.setError("Invalid password");
ussd_ok.setEnabled(false);
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (ussd_edittext.getText().toString().trim().replaceAll("[^0-9#*]", "").length() < 4) {
ussd_edittext.setError("USSD code must contain * and #");
ussd_ok.setEnabled(false);
}
}
public void afterTextChanged(Editable s) {
Character cr = s.toString().charAt(0);
if(s.length() < 1)
{
if(!( (cr == '*') ) )
{ ussd_edittext.setError("USSD code must contain * and #");
ussd_ok.setEnabled(false);
}
else {
ussd_ok.setEnabled(true);
}
}
else
{
String lc = s.toString().substring(0, ussd_edittext.length() - 1);
if(!( (cr == '*') && lc.equals("#") ) )
{
ussd_edittext.setError("USSD code must contain * and #");
ussd_ok.setEnabled(false);
}
else {
ussd_ok.setEnabled(true);
}
}
}
});
我想验证它必须包含 * 和 # 也是最少 5 位数字。
【问题讨论】:
标签: android validation android-edittext