如果字符串包含0-9其他字符的Android检查 [英] android check if string contains characters other than 0-9
问题描述
我创造了游戏计数点的应用程序。这个程序有一个的EditText组件。我要检查如果从编辑文本检索到的字符串包含0-9其它字符。这是因为我的应用程序包含一个integer.parse功能至极崩溃,如果不是0-9其它字符便被输入。所有帮助将大大AP preciated。先谢谢了。
I am creating an app for counting points in games. This app has a edittext component. I want to check if the string retrieved from the edit text contains characters other than 0-9. This is because my app contains a integer.parse function wich crashes if characters other than 0-9 is inputed. All help will be greatly appreciated. Thanks in advance.
推荐答案
如果你只是想通知无效字符的用户,那么你可以在的try / catch把它包
并据此采取行动。
If you just want to notify the user of an invalid character then you can wrap it in a try/catch
and act accordingly
try
{
int someInt = Integer.parseInt(et.getText().toString());
// other code
}
catch (NumberFormatException e)
{
// notify user with Toast, alert, etc...
}
您也可以使用定期前pression 找你要根据你的需要/不想要的字符。
You also can use a regular expression to look for the characters you want/don't want depending on your needs.
只是要万一我的code注释没有,我建议你做一些不同之处并通知用户清除即可。不要抓住它,让它坐
Just to be clear in case my code comment wasn't, I am suggesting that you do something with the exception and notify the user. Don't catch it and let it sit
这篇关于如果字符串包含0-9其他字符的Android检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!