How can Android determine if the content is a number?

In Android, you can use the TextUtils.isDigitsOnly() method to determine if a string is a number. This method checks each character in the string to see if it is a digit, and will return true if all characters are digits, otherwise it will return false.

Here is the example code:

String str = "12345";
if(TextUtils.isDigitsOnly(str)) {
    Log.d("TAG", "字符串是数字");
} else {
    Log.d("TAG", "字符串不是数字");
}

This makes it very convenient to determine whether a string is a number.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds