Java 使用setText()使用固定字符串的动态计数器?
我正在尝试实现一种计算按钮点击次数的方法。该方法返回字符串“Сlicked X times”。X是单击次数。第一次单击后,它返回一条文本视图消息“单击1次”。第二次单击关闭应用程序。错误:输入字符串“已检查1次”的NumberFormatException。如何用字符串对数字进行动态更新?如果没有字符串“checked”和“times”,它会正确地增加数字Java 使用setText()使用固定字符串的动态计数器?,java,android,Java,Android,我正在尝试实现一种计算按钮点击次数的方法。该方法返回字符串“Сlicked X times”。X是单击次数。第一次单击后,它返回一条文本视图消息“单击1次”。第二次单击关闭应用程序。错误:输入字符串“已检查1次”的NumberFormatException。如何用字符串对数字进行动态更新?如果没有字符串“checked”和“times”,它会正确地增加数字 private void counterMethod(View v) { String countValue =
private void counterMethod(View v) {
String countValue = numberOfClicks.getText().toString();
int intCountValue = Integer.parseInt(countValue);
intCountValue++;
String stringCountValue=String.valueOf(intCountValue);
numberOfClicks.setText("checked "+stringCountValue +" times");
}
你试过了吗
numberOfClicks.setText("checked $intCountValue times");
你试过了吗
numberOfClicks.setText("checked $intCountValue times");
在解析字符串之前,使用
replaceAll()
从字符串中删除所有非数字字符:
String countValue = numberOfClicks.getText().toString();
if (countValue.trim().length() == 0) countValue = "0";
int intCountValue = Integer.parseInt(countValue.replaceAll("\\D", ""));
在解析字符串之前,使用
replaceAll()
从字符串中删除所有非数字字符:
String countValue = numberOfClicks.getText().toString();
if (countValue.trim().length() == 0) countValue = "0";
int intCountValue = Integer.parseInt(countValue.replaceAll("\\D", ""));
第二次单击关闭应用程序,因为在第二次单击期间,
countValue
将保存一个非整数字符串
String countValue = numberOfClicks.getText().toString();
// the above variable will be holding the string "checked 1 times"
// When parsing this to Integer, the application throws an error in
// the following line.
int intCountValue = Integer.parseInt("checked 1 times");
要解决此问题,您必须单独保留计数,而不是从文本字段中获取计数。第二次单击关闭应用程序,因为在第二次单击期间,
countValue
将保存一个非整数字符串
String countValue = numberOfClicks.getText().toString();
// the above variable will be holding the string "checked 1 times"
// When parsing this to Integer, the application throws an error in
// the following line.
int intCountValue = Integer.parseInt("checked 1 times");
要解决此问题,您必须单独保留计数,而不是从文本字段中获取计数。您可以这样尝试:
int intCountValue =0;
private void counterMethod(View v) {
intCountValue++;
numberOfClicks.setText("checked "+intCountValue +" times");
}```
您可以这样尝试:
int intCountValue =0;
private void counterMethod(View v) {
intCountValue++;
numberOfClicks.setText("checked "+intCountValue +" times");
}```
不幸的是,它不起作用。。。。第一次单击后返回“checked$intCountValue times”。不幸的是,它无法工作。。。。第一次点击后返回“checked$intCountValue times”。非常感谢!它起作用了!String countValue=numberOfClicks.getText().toString().replaceAll(“[^\\d.]”,“”);如果文本视图为空,请检查我的编辑。如果文本视图为空,则编辑无效。在第一次单击之前,它必须为空。逻辑上但是现在计数器并没有增加这个数字。我不明白为什么……第一个解决方案是使用具有“0”的TextView。如果TextView为空,则If语句将生成
countValue=“0”
,解析将生成int intCountValue=0
sointCountValue++代码>将使intCountValue=1
。那么什么不起作用呢?我删除了“intCountValue++”…我的错误。现在它工作得很好!谢谢!!!!!谢谢!它起作用了!String countValue=numberOfClicks.getText().toString().replaceAll(“[^\\d.]”,“”);如果文本视图为空,请检查我的编辑。如果文本视图为空,则编辑无效。在第一次单击之前,它必须为空。逻辑上但是现在计数器并没有增加这个数字。我不明白为什么……第一个解决方案是使用具有“0”的TextView。如果TextView为空,则If语句将生成countValue=“0”
,解析将生成int intCountValue=0
sointCountValue++代码>将使intCountValue=1
。那么什么不起作用呢?我删除了“intCountValue++”…我的错误。现在它工作得很好!谢谢!!!!!非常感谢:)方法.replaceAll([^\\d.]”帮助了我。非常感谢:)方法.replaceAll([^\\d.]”帮助了我。numberOfClicks是我的TextView。它包含“0”。但是如果TextView为空,我需要一个解决方案。使用方法getText()我从TextView获取值。您的“intCountValue”未与TextView链接。它将不起作用。numberOfClicks是我的TextView。它包含“0”。但如果TextView为空,我需要一个解决方案。使用方法getText()我从TextView获取值。您的“intCountValue”未与TextView链接。它将不起作用。