我在两个按钮上有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()
}
=================
答案 0 :(得分:-1)
在您的方法中执行此操作。这样的事情:
public class ChangeCell{
public ChangeCell(){
try
{
File file = new File("C:\\ENTWICKLUNG\\testfolder\\out\\test.xlsx");
FileInputStream fis = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFWorkbook newWb = new XSSFWorkbook();
XSSFCell oldCell = wb.getSheetAt(0).getRow(0).getCell(0);
XSSFSheet newSheet = newWb.createSheet("Test");
XSSFRow newRow = newSheet.createRow(0);
XSSFCell newCell = newRow.createCell(0);
newCell.setCellValue("TestForCell");
XSSFCellStyle newCellStyle = newWb.createCellStyle();
cellChange(oldCell, newCell, newCellStyle);
newWb.write(new FileOutputstream("newTest.xlsx");
newWb.close();
}
//.. catch omitted
}
private void cellChange(Cell cell, XSSFCell newCell, XSSFCellStyle newCellStyle)
{
try
{
XSSFCell tt = (XSSFCell) cell;
XSSFColor color = tt.getCellStyle().getFillForegroundColorColor();
newCellStyle.setFillForegroundColor(color);
newCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
newCell.setCellStyle(newCellStyle);
}
//.. catch omitted
} }
并且应该像这样调用它:
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.
}
<强> == EDIT ======= 强>
您的功能应如下所示:
calculateBtn.setOnClickListener{
calculation(weightView.getText.toString(),percentageView.getText.toString())
}
你的onClick应该是这样的:
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
}