登山则情满于山,观海则意溢于海。这篇文章主要讲述计算百分比并在textview android studio中显示相关的知识,希望能为你提供帮助。
我正在开发一个应用程序,我将1个变量int通过一个活动字符串传递给下一个活动,在下一个活动中我接受该字符串并再次返回它并返回一个int,然后我计算一个百分比并显示和一个textview,过去变量和大约1所以我检查它是否为空,然后我计算百分比ex:((3/45)* 100)并在文本视图中显示,再次查看字符串...但无论如何我犯了一个错误什么可以?
Bundle bundle = getIntent (). GetExtras ();
String aprox1 = bundle.getString ("aprox1");
if (aprox1! = null)
try {
num3 = Integer.parseInt (aprox1);
result = Math.round ((num3 / 45) * 100);
TextView counter2 = (TextView) findViewById(R.id.textView16);
String abcString2 = Integer.toString (result);
counter2.setText (abcString2);
}
catch (NumberFormatException e) {
}
答案你需要在if语句之后加{},如下所示:
if (aprox1! = null) {
try {
num3 = Integer.parseInt (aprox1);
result = Math.round ((num3 / 45) * 100);
TextView counter2 = (TextView) findViewById(R.id.textView16);
String abcString2 = Integer.toString (result);
counter2.setText (abcString2);
}
catch (NumberFormatException e) {
}
}
还值得注意的是,因为当你将它除以45时,num3是一个整数,你将获得一个整数而不是一个百分比。
要解决此问题,请在计算除法之前将num3设为double或将num3或45转换为double。
【计算百分比并在textview android studio中显示】例如,一个简单的修复方法包括将第4行更改为以下内容:
result = Math.round ((num3 / 45.0) * 100);
推荐阅读
- 使用android中的颜色选择器更改textview的文本颜色和背景颜色
- AWSAppSync GraphQL突变执行两次
- 为什么webview头键在android中以小写形式转换()
- Xamarin Android - Task.Run vs Task.Factory.StartNew和Thread.CurrentPrincipal
- 在VS 2017 xamarin android项目中没有击中断点
- 同步/阻止Application.Invoke()for GTK#
- 如何在Cordova中检索应用程序版本,应用程序名称,程序包名称,版本代码和版本号
- 5个最佳网页加载进度条javascript插件
- 5+最佳的jquery和javascript datepicker插件