Android – 使功能为false /不运行
我有两个按钮上的setOnClickListener
,它们只是在两个函数中运行代码。
我想创建一个安全的电话,所以当我点击按钮的应用程序不会崩溃。 我以为我不得不让他们虚假或什么,但显然这是行不通的。
我应该怎么做?
谢谢
if (weightView.text.isEmpty() && percentageView.text.isEmpty()) { calculation() == false lbsCalculation() == false } else { calculation() lbsCalculation() }
这些是我的两个clickListeners
calculateBtn.setOnClickListener{ calculation() } lbsCalculationBtn.setOnClickListener{ lbsCalculation() }
功能:
fun calculation () { var weightValue = weightView.text.toString().toInt() var percentageValue = percentageView.text.toString().toInt() var result = (weightValue * percentageValue) / 100.toDouble() var resultFormat = "%.1f KG".format(result) resultView.text = resultFormat.toString() } fun lbsCalculation() { var weightValue = weightView.text.toString().toInt() var percentageValue = percentageView.text.toString().toInt() var result = ((weightValue * percentageValue) / 100) * 2.2.toDouble() var resultFormat = "%.1f LBS".format(result) resultView.text = resultFormat.toString() }
=================
图片
在你的方法里面做。 像这样的东西:
public class calculation(String weightView, String percentageView){ //you make a condition that should do something if strings are empty if(weightView && percentageView == null) //DO WHAT YOU WANT }else{ //Show an alert or something that your EditText is empty. }
应该像这样调用它:
calculateBtn.setOnClickListener{ calculation(weightView.getText.toString(),percentageView.getText.toString()) }
======编辑=======
你的功能应该是这样的:
fun calculation(weightValue: String,percentageValue: String) { if(weightValue == null && percentageValue == null ){ var myWeightValue = weightValue.toInt(); var myPercentageValue = percentageValue.toInt(); var result = (myWeightValue * myPercentageValue) / 100 var resultFormat = "%.1f KG".format(result) resultView.text = resultFormat } else { **SHOW A MESSAGE LIKE A TOAST OR ALERTDIALOG TO LET THE USER KNOW THAT WEIGHT AND PERCENTAGE IS REQUIRED** } } fun lbsCalculation() { var weightValue = weightView.text.toString().toInt() var percentageValue = percentageView.text.toString().toInt() var result = ((weightValue * percentageValue) / 100) * 2.2 var resultFormat = "%.1f LBS".format(result) resultView.text = resultFormat }
你的onClick应该是这样的:
calculateBtn.setOnClickListener{ calculation(weightView.text,percentageView.text) }
- 在Android中将地图转换为捆绑
- 如何写while循环与在kotlin中的responseInputStream.read – (while((i = responseInputStream.read(byteContainer))
- Kotlin的数据类,Android室和自定义setter
- 使用Android Studio 3.0 beta7版本时,Kotlin注释处理器会导致编译时错误
- 如何使用drawLine()绘制线条
- Kotlin Date.daysAfter函数具有相同参数的不同返回值
- 创建一个微调器,当选择一个项目时调整它的宽度
- 如何将多个历史数据添加到GoogleFit
- Android – 在Kotlin中声明和使用视图