EditText怎么控制不让输入0开头的数字?

开发第二个应用时候又到了EditText首字母限制,查了摆渡,找到了这种监听后重写方法的办法

下面附上代码:

现在初始化时候添加监听

EditText.addTextChangeListener(mTextWatcher);

//在一开始声明TextWatcher,在afterTextChange内操作
private TextWatcher mTextWatcher=new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
String text = s.toString();
int len = s.toString().length();
if (len == 1 && text.equals("0")) {
s.clear();

}
}
};



给EditView添加TextWatcher@Overridepublic void afterTextChanged(Editable s){ String text = s.toString(); int len = s.toString().length(); if (len == 1 && text.equles("0")) { s.clear(); }}

你可能感兴趣的:(监听,输入0)