radioClick()返回值1为正确的答案。我不希望Toast出现,所以我尝试删除该方法,但现在radioClick()不会返回值1.任何帮助将不胜感激。
package com.example.android.justjava;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.text.NumberFormat;
/**
* This app displays a quiz about Kankakee County
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createRadioButtons();
}
/**
* @param numOfExits is the value passed to the exitsCorrect() method
* @param answerToQuestionOne is the value passed to the schuyler() method
*/
int numOfExits = 0;
String answerToQuestionOne = "";
/**
* METHODS WHICH RETURN POINTS FOR CORRECT ANSWERS ARE FOUND BELOW
*/
/** QUESTION 1
* This method is called to return 1 point if Schuyler Avenue is selected
* @param answerToQuestionOne
* @return
*/
public int schuyler(String answerToQuestionOne) {
if ("Schuyler".equals(answerToQuestionOne)) {
return 1;
} else {
return 0;
}
}
/** QUESTION 2
* This method calculates whether the correct two boxes were checked and returns 1 point if so
*
* @param kccGuessed is whether KCC was guessed
* @param cobbGuessed is whether Cobb Park was guessed
* @param depotGuessed is whether the Train Depot was guessed
* @param majesticGuessed is whether Majestic Theatre was guessed
*/
private int gazebo(boolean kccGuessed, boolean cobbGuessed, boolean depotGuessed, boolean majesticGuessed) {
if (cobbGuessed && depotGuessed && !kccGuessed && !majesticGuessed ) {
return 1;
} else {
return 0;
}
}
/** QUESTION 3
* This method awards a point if "2" is selected as the number of exits in Kankakee
*/
public int exitsCorrect(int numOfExits) {
if (numOfExits == 2) {
return 1;
} else {
return 0;
}
}
这是按下正确的单选按钮时应该返回1的方法。
/** QUESTION 4
*
* this method returns 1 point if Maternity BVM is selected
*/
public int radioClick() {
RadioGroup group = (RadioGroup) findViewById(R.id.myRadioGroup);
int idOfSelected = group.getCheckedRadioButtonId();
if (idOfSelected == 1){
return 1;
} else {
return 0;
}
}
/**
* END RETURN METHODS
*/
/**
* METHODS THAT ASSIST WITH QUIZ QUESTIONS ARE FOUND BELOW
*/
/** ASSIST QUESTION 3
* This method is called when the increment button is clicked.
*/
public void increment(View view) {
if (numOfExits < 5) {
numOfExits++;
displayNumOfExits(numOfExits);
}
}
/** ASSIST QUESTION 3
* This method is called when the decrement button is clicked.
*/
public void decrement(View view) {
if (numOfExits > 0) {
numOfExits--;
displayNumOfExits(numOfExits);
}
}
/** CREATE QUESTION 4
* This method creates the radio button dynamically
*/
public void createRadioButtons() {
RadioGroup group = (RadioGroup) findViewById(R.id.myRadioGroup);
final String[] radio = getResources().getStringArray(R.array.radio_string_array);
//Create the buttons
for (int i = 0; i < radio.length; i++) {
final String radioString = radio[i];
RadioButton button = new RadioButton(this);
button.setText(getString(R.string.radio_selected, radioString));
我尝试删除以下Toast,但只有当包含Toast的行存在时才会返回1.
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, getString(R.string.you_clicked) + radioString, Toast.LENGTH_SHORT)
.show();
}
});
// Add to radio group
group.addView(button);
}
}
/**
* END QUIZ QUESTION ASSISTS
*/
/**
* This method is called when the submit button is clicked.
*/
public void submitOrder(View view) {
EditText questionOneAnswer = (EditText) findViewById(R.id.question_one_answer);
answerToQuestionOne = questionOneAnswer.getText().toString();
CheckBox kccCheckBox = (CheckBox) findViewById(R.id.kcc_checkbox);
boolean kccGuessed = kccCheckBox.isChecked();
CheckBox cobbCheckBox = (CheckBox) findViewById(R.id.cobb_checkbox);
boolean cobbGuessed = cobbCheckBox.isChecked();
CheckBox depotCheckBox = (CheckBox) findViewById(R.id.depot_checkbox);
boolean depotGuessed = depotCheckBox.isChecked();
CheckBox majesticCheckBox = (CheckBox) findViewById(R.id.majestic_checkbox);
boolean majesticGuessed = majesticCheckBox.isChecked();
int gazeboCorrect = gazebo(kccGuessed, cobbGuessed, depotGuessed, majesticGuessed);
int numExitsCorrect = exitsCorrect(numOfExits);
int schuylerCorrect = schuyler(answerToQuestionOne);
int radioCorrect = radioClick();
String priceMessage = createQuizSummary(gazeboCorrect, numExitsCorrect, schuylerCorrect, radioCorrect);
TextView orderSummaryTextView = (TextView) findViewById(R.id.result_text_view);
orderSummaryTextView.setText(priceMessage);
}
/**
*
* @param schuylerCorrect is whether or not question 1 was answered correctly
* @param gazeboGuessed is whether or not question 2 was answered correctly
* @param exitsGuessed is whether or not question 3 was answered correctly
* @param radioCorrect is whether or not question 4 was answered correctly
* @return String
*/
public String createQuizSummary(int schuylerCorrect, int gazeboGuessed, int exitsGuessed, int radioCorrect) {
int total = schuylerCorrect + gazeboGuessed + exitsGuessed + radioCorrect;
String message = "";
if (total > 3) {
message = "Congratulations, you got " + total + " correct!";
} else if (total > 2) {
message = "Not bad, you got " + total + " correct!";
} else if (total > 1) {
message = "Meh, you did OK...I guess...\nNext time try to get more than " + total
+ " correct!";
} else if (total > 0) {
message = "Crap on a crutch! You only got " + total + " right!";
} else {
message = "You didn't get any right! What are you, a yellow hammer?!";
}
return message;
}
/**
* This method displays the number of exits on the screen.
*/
private void displayNumOfExits(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.exits_text_view);
quantityTextView.setText("" + number);
}
}