我有一个 [DONE] 按钮,用于检查 2 个条目的结果。其中一个是[EditText--inputType:number],另一个是[TextView],当按下某个按钮时会增加。
我想要做的是检查 EditText 是否有整数或为空,并检查 TextView 的内容。如果它们都大于零。我将它们加起来并将总数发送到我的主要活动。这是我到目前为止的代码。
public void returnbtn(View view) {
// Initialize insert textView
EditText insertcountBtn = findViewById(R.id.insertPushup);
// Initialize counter textView
TextView givencountBtn = findViewById(R.id.showCount);
// get added int stuff from the insert textField
int insertcountInt =
Integer.parseInt(insertcountBtn.getText().toString());
// get string stuff from counter textView
String givencountString = givencountBtn.getText().toString();
// convert counter textView to int.
Integer givencountInt = Integer.parseInt(givencountString);
if (givencountInt <= 0 && insertcountInt <= 0){
Total = 0;
} else if (givencountInt > 0 && insertcountInt <= 0) {
Total = givencountInt;
} else if (givencountInt <= 0 && insertcountInt > 0) {
Total = insertcountInt;
} else if (givencountInt > 0 && insertcountInt > 0){
// Add Counter textView and Insert textView to an Int Total
Total = givencountInt + insertcountInt;
}
// Create an Intent to return to the mainActivity.
Intent beginPushup = new Intent(this, MainActivity.class);
// Pass the current number to the push-up Counter activity.
beginPushup.putExtra(TOTAL_DONE, Total);
// Start mainActivity.
startActivity(beginPushup);
}
我遇到的问题是 textView 或 EditText 我不确定。我所知道的是,如果我同时填写它们并单击完成,它会将它们相加并按预期将总数转移到 mainActivity。如果我将值添加到 EditText 并将 TextView 保留为 0,它也会执行它的意图。但是,如果我增加 TextView 并将 EditText 留空,它不会传输我的 TextView 整数并使应用程序崩溃。
我是否可以检测到 editText 错误,因为我认为这就是原因。如果是这样,正确的方法是什么?
慕哥6287543
九州编程
三国纷争
相关分类