Java Android-添加按钮标题时的随机整数值
我使用一堆布尔值,根据这些布尔值的结果,将整数值设置为一定的值。此代码运行良好。我提供的代码的第一行显示了“completedtotal”和“total”的正确值。但当我将文本添加到按钮时,它将显示随机数。更糟糕的是,它在我的水平进度条中根本没有显示任何值Java Android-添加按钮标题时的随机整数值,java,android,button,android-progressbar,Java,Android,Button,Android Progressbar,我使用一堆布尔值,根据这些布尔值的结果,将整数值设置为一定的值。此代码运行良好。我提供的代码的第一行显示了“completedtotal”和“total”的正确值。但当我将文本添加到按钮时,它将显示随机数。更糟糕的是,它在我的水平进度条中根本没有显示任何值 Log.d("MyLog", "The value of completed is " + completedtotal + " and total is " + total); int temp = completedtotal / tot
Log.d("MyLog", "The value of completed is " + completedtotal + " and total is " + total);
int temp = completedtotal / total;
temp = temp * 100;
String tempString = completedtotal + "/" + total + " Challenges Completed";
Button b = (Button) findViewById(R.id.button1);
b.setText(tempString);
ProgressBar pb = (ProgressBar) findViewById(R.id.prog);
Log.d("Mylog", "value is " + temp);
pb.setProgress(temp);
谢谢你 试试这个:
String tempString = ""+completedtotal + "/" + total + " Challenges Completed";
或
因为第一个值是整数而不是字符串,所以我想这可能会产生问题。请尝试以下方法:
Button b = (Button) findViewById(R.id.button1);
b.setText(String.valueOf(completedtotal) + "/" + String.valueOf(total) + " Challenges Completed");
查找ascii字符,该字符由您得到的“随机值”表示。String.valueOf(total)——我想不需要将其转换为String,因为它位于String之间,所以它将自动转换为String。不,仍然在做同样的事情。奇怪的是,代码开头的log cat语句返回100%的正确值。。。还有,进度条上有什么想法?谢谢@TimJ看到Kai的答案对设置进度条很有用,而我的答案对设置按钮文本很有用。不,仍然在做同样的事情。奇怪的是,代码开头的log cat语句返回100%的正确值。。。还有,进度条上有什么想法?谢谢completedtotal和total是整数还是浮点数?对不起,两者都是整数
Button b = (Button) findViewById(R.id.button1);
b.setText(String.valueOf(completedtotal) + "/" + String.valueOf(total) + " Challenges Completed");