I have an application and noticed that I didn't include validation for a string entry. When the user clicks a button I want the button which is an onclick method to check if the entry within an editText is a string or an integer.
Overall I want to be able to only allow strings to pass through from an editText not开发者_如何学Python integers.
Thanks
there are many ways how to do that. One of them is to match the regular expression:
Button b = (Button)findViewById(R.id.btn);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText et = (EditText)findViewById(R.id.edit);
String str = et.getText().toString();
if(str.matches("[0-9]*")) {
// no integers allowed - no further processing
return;
}
...
}
});
精彩评论