1

如何检查 TextField 是否仅包含 (DOT) 字符。TextField 可以包含以下内容...

.9 = > Valid
9. = > Valid
9.23 = >Valid

但是,如果 TextField 仅包含 (.),则应该向用户抛出错误消息。我不能做Text.startsWith()或都不做Text.contains(),因为在其他情况下两者都可能失败。

4

4 回答 4

4

使用这个正则表达式

text.matches("\\d+|\\d*\\.\\d+|\\d+\\.\\d*")
  • \\d+仅匹配数字
  • \\d*\\.\\d+匹配以点或数字开头并包含点的字符串
  • \\d+\\.\\d*匹配以 digit 开头、包含点和其后任意数量的数字的字符串

请记住,此表达式也接受以 0 开头的数字。

于 2013-07-05T10:47:41.503 回答
1

你可以简单地使用

text.equals(".")

或者,考虑到空格,

text.trim().equals(".")
于 2013-07-05T10:12:29.167 回答
1

如果您还必须检查除了点之外还有一个数字,您可能应该编写几个正则表达式。

在您的情况下,如果字符串与以下模式匹配,则该字符串是有效的:

\d+       // Digits without the dot
\d*\.\d+  // Number with some digits after the dot
\d+\.\d*  // Number with some digits before the dot
于 2013-07-05T10:15:13.857 回答
-1

你可以使用两种方法

if(yourTextFieldName.getText().endsWith(".") && yourTExtFieldName.startsWith(".")) {
    //write your code here
}
于 2013-07-05T10:29:16.163 回答