【发布时间】:2019-02-21 16:07:04
【问题描述】:
我有一个 [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 错误吗,因为我认为这就是原因。 如果是这样,正确的方法是什么?
----- 第一个 && 第二个答案编辑 -----
int insertcountInt;
String insertcountString =
String.valueOf(insertcountBtn.getText());
try {
insertcountInt =
Integer.parseInt(insertcountBtn.getText().toString());
if (insertcountInt <= 0 || insertcountString == " ") Total =
givencountInt;
if (insertcountInt > 0 ) Total = givencountInt +
insertcountInt;
} catch (NumberFormatException e) {
insertcountInt = 0;
}
好消息是不再崩溃,但除非我用一些东西填充 EditText,否则我不会在 mainActivity 中收到我的 Integer。这越来越有趣了。 * 我认为这是我已经重组的 if 语句的游戏,但到目前为止还没有运气。 *
----- 更新了任何未来请求的工作代码 -----
public void filledChecker() {
// Initialize insert textView
EditText insertcountBtn =
findViewById(R.id.insertPushup);
// Initialize counter textView
TextView givencountBtn = findViewById(R.id.showCount);
int insertcountInt = 0;
int givencountInt = 0;
// get added int stuff from the insert textField
if (!TextUtils.isEmpty(insertcountBtn.getText()) &&
TextUtils.isDigitsOnly(insertcountBtn.getText())) {
insertcountInt =
Integer.parseInt(insertcountBtn.getText().toString());
}
// get string stuff from counter textView
String givencountString = givencountBtn.getText().toString();
if (!TextUtils.isEmpty(givencountString) &&
TextUtils.isDigitsOnly(givencountString)) {
givencountInt = Integer.parseInt(givencountString);
}
if (insertcountInt <= 0) {
Total = givencountInt;
}
if (insertcountInt > 0) {
Total = givencountInt + insertcountInt;
}
}
public void returnbtn(View view) {
// Create an Intent to return to the mainActivity.
Intent beginPushup = new Intent(this, MainActivity.class);
filledChecker();
Integer current_Id = getIntent().getIntExtra(GIVEN_ID, 0);
// Pass the current number to the push-up Counter activity.
if (current_Id == 1) beginPushup.putExtra(TOTAL_P_DONE,
Total);
if (current_Id == 2) beginPushup.putExtra(TOTAL_S_DONE,
Total);
if (current_Id == 3) beginPushup.putExtra(TOTAL_C_DONE,
Total);
if (current_Id == 4) beginPushup.putExtra(TOTAL_SQ_DONE,
Total);
beginPushup.putExtra(GIVEN_ID, current_Id);
// Start mainActivity.
startActivity(beginPushup);
}
I 如您所见,我最终将 if 逻辑从 Intent 转移到主 Activity。这样他们都很简单。并且使用 Do Not Repeat Yourself 我对其他按钮做了同样的事情,正如您在 [returnbtn] 方法中看到的所有 if 一样。除了我得到帮助的那些之外,我还简化了 if 语句。我最终需要 2 个 if 语句来管理从 2 个条目中获取总数。再次感谢大家的帮助。哦,try-except 似乎没有必要,所以我弃用了它们。随着应用变得越来越复杂,我会在必要时添加它们。
【问题讨论】:
-
你能发布堆栈跟踪吗?
-
代码完全没问题。当我在我的虚拟设备上运行它时发生崩溃。这是否准确地回答了您的问题?或者还有其他你想看的代码。我不确定在哪里可以找到此类崩溃的错误日志。
-
可以这么说,我所有的 gradle 构建都完全没有错误。 :D
标签: java android android-intent integer