开发者

how to validate a string entry on Editext within the click of a button

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;
        }                                   
        ...
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜