Android 单击“下一步”按钮时,如何检查是否选中了任何单选按钮?
我正在开发一款问答游戏android应用程序,其中包含带有单选按钮和按钮(下一步)的问题。因此,当按下按钮时,我必须检查用户是否从选项中选择答案。如果没有,则应弹出一条警告消息,说明“在继续下一个问题之前,您必须选择答案”。谁能帮帮我吗! 下面是Java代码Android 单击“下一步”按钮时,如何检查是否选中了任何单选按钮?,android,Android,我正在开发一款问答游戏android应用程序,其中包含带有单选按钮和按钮(下一步)的问题。因此,当按下按钮时,我必须检查用户是否从选项中选择答案。如果没有,则应弹出一条警告消息,说明“在继续下一个问题之前,您必须选择答案”。谁能帮帮我吗! 下面是Java代码 public class Main2Activity extends AppCompatActivity { Button button2; private TextView countLabel; private
public class Main2Activity extends AppCompatActivity {
Button button2;
private TextView countLabel;
private TextView questionLabel;
private RadioButton answerBtn1;
private RadioButton answerBtn2;
private RadioButton answerBtn3;
private RadioButton answerBtn4;
private String rightAnswer;
private int rightAnswerCount = 0;
private int quizCount = 1;
static final private int QUIZ_COUNT = 10;
ArrayList<ArrayList<String>> quizArray = new ArrayList<>();
String quizData[][] = {
//{"Question", "Right Answer", "Choice1", "Choice2", "Choice3"}
{"It is a stack of software for mobile devices which includes an Operating System, middleware and some key applications.", "Android", "Intent", "Toast", "Service"},
{"Developed by Microsoft.", "Windows", "Android", "Blackberry", "iOS"},
{"Used to build apps and run them directly on Apple devices.", "Xcode", "Android Studio", "Visual Studio", "Windows"},
{"Represents a single screen with a user interface", "Activity", "Service", "Content Provider", "Broadcast receivers"},
{"A component that runs in the background to perform operations or to perform work for remote processes", "Service", "Activity", "Content Provider", "Broadcast receivers"},
{"Managers a shared set of app data", "Content Provider", "Activity", "Service", "Broadcast receiver"},
{"Responds to system-wide Broadcast announcements", "Broadcast receivers", "Activity", "Content Provider", "Service"},
{"It is connected to either the external world of application or internal world of application", "Intent", "Manifest File", "Resources", "Broadcast receiver"},
{"It is a collection of views and other child views, it is an invisible part and the base class for layouts.", "viewGroup", "view", "Relative Layout", "Linear Layout"},
{"It will show a pop up message on the surface of the window", "Toast Notification", "Status Bar Notification", "Dialogue Notification", "Action Bar"},
{"It will show notifications on status bar", "Status Bar Notification", "Toast Notification", "Dialogue Notification", "Action Bar"},
{"It is an activity related notification.", "Dialogue Notification", "Toast Notification", "Status Bar Notification", "Action Bar"},
{"Contains code to test your application projects", "Test Module", "Library Module", "App Engine Module", "Android App Module"},
{"Cannot be installed onto a device", "Library Module", "Test Module", "Android App Module", "App Engine Module"},
{"Allows implementation of functionality such as Real-time interactions", "App Engine Module", "Test Module", "Library Module", "Android App Module"}
};`enter code here`
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
countLabel = (TextView) findViewById(R.id.countLabel);
questionLabel = (TextView) findViewById(R.id.questionLabel);
answerBtn1 = (RadioButton) findViewById(R.id.answerBtn1);
answerBtn2 = (RadioButton) findViewById(R.id.answerBtn2);
answerBtn3 = (RadioButton) findViewById(R.id.answerBtn3);
answerBtn4 = (RadioButton) findViewById(R.id.answerBtn4);
button2 = (Button) findViewById(R.id.button2);
for (int i = 0; i < quizData.length; i++) {
ArrayList<String> tmpArray = new ArrayList<>();
tmpArray.add(quizData[i][0]);
tmpArray.add(quizData[i][1]);
tmpArray.add(quizData[i][2]);
tmpArray.add(quizData[i][3]);
tmpArray.add(quizData[i][4]);
quizArray.add(tmpArray);
}
showNextQuiz();
}
public void showNextQuiz() {
countLabel.setText("Question No." + "" + quizCount);
Random random = new Random();
int randomNum = random.nextInt(quizArray.size());
ArrayList<String> quiz = quizArray.get(randomNum);
questionLabel.setText(quiz.get(0));
rightAnswer = quiz.get(1);
quiz.remove(0);
Collections.shuffle(quiz);
answerBtn1.setText(quiz.get(0));
answerBtn2.setText(quiz.get(1));
answerBtn3.setText(quiz.get(2));
answerBtn4.setText(quiz.get(3));
answerBtn1.setChecked(false);
answerBtn2.setChecked(false);
answerBtn3.setChecked(false);
answerBtn4.setChecked(false);
quizArray.remove(randomNum);
}
public void checkAnswer(final View view) {
button2.setOnClickListener(new View.OnClickListener() {
final RadioButton answerBtn = (RadioButton) findViewById(view.getId());
String btnText = answerBtn.getText().toString();
private Boolean validationSuccess() {
if(answerBtn.isChecked()==false){
alertDialog();
return false;
}
return true;
}
@Override
public void onClick(View v) {
if (btnText.equals(rightAnswer)) {
rightAnswerCount++;
}else{
validationSuccess();
}
if (quizCount == QUIZ_COUNT) {
Intent intent = new Intent(getApplicationContext(), Main3Activity.class);
intent.putExtra("RIGHT_ANSWER_COUNT", rightAnswerCount);
startActivity(intent);
} else {
quizCount++;
showNextQuiz();
}
}
});
}
private void alertDialog() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
Main2Activity.this);
alertDialogBuilder.setMessage("Please ensure that you choose your answer!")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
}
公共类Main2活动扩展了AppCompative活动{
按钮2;
私有文本视图标签;
私有文本视图问题标签;
专用无线按钮应答器BTN1;
专用无线按钮应答器BTN2;
专用无线按钮应答器BTN3;
专用无线按钮应答器BTN4;
私有字符串右应答;
private int rightAnswerCount=0;
私有整数quizCount=1;
静态最终私人智力测验计数=10;
ArrayList quizArray=新的ArrayList();
字符串quizData[][]{
//{“问题”、“正确答案”、“选择1”、“选择2”、“选择3”}
{“它是一个用于移动设备的软件堆栈,包括操作系统、中间件和一些关键应用程序。”、“Android”、“Intent”、“Toast”、“Service”},
{“由微软开发”,“Windows”,“安卓”,“黑莓”,“iOS”},
{“用于构建应用程序并直接在苹果设备上运行。”、“Xcode”、“Android Studio”、“Visual Studio”、“Windows”},
{“表示具有用户界面的单个屏幕”、“活动”、“服务”、“内容提供商”、“广播接收器”},
{“在后台运行以执行操作或为远程进程执行工作的组件”、“服务”、“活动”、“内容提供商”、“广播接收器”},
{“管理一组共享的应用程序数据”、“内容提供商”、“活动”、“服务”、“广播接收器”},
{“响应全系统广播公告”、“广播接收器”、“活动”、“内容提供商”、“服务”},
{“它连接到应用程序的外部世界或应用程序的内部世界”、“意图”、“清单文件”、“资源”、“广播接收器”},
{“它是视图和其他子视图的集合,是不可见的部分,是布局的基类。”、“视图组”、“视图”、“相对布局”、“线性布局”},
{“它将在窗口表面显示弹出消息”、“Toast通知”、“状态栏通知”、“对话通知”、“操作栏”},
{“它将在状态栏上显示通知”、“状态栏通知”、“Toast通知”、“对话通知”、“操作栏”},
{“这是一个与活动相关的通知”,“对话通知”,“Toast通知”,“状态栏通知”,“操作栏”},
{“包含测试应用程序项目的代码”、“测试模块”、“库模块”、“应用程序引擎模块”、“Android应用程序模块”},
{“无法安装到设备”、“库模块”、“测试模块”、“Android应用程序模块”、“应用程序引擎模块”},
{“允许实现实时交互”、“应用程序引擎模块”、“测试模块”、“库模块”、“Android应用程序模块”等功能}
}在这里输入代码`
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
countLabel=(TextView)findViewById(R.id.countLabel);
questionLabel=(TextView)findViewById(R.id.questionLabel);
answerBtn1=(单选按钮)findViewById(R.id.answerBtn1);
answerBtn2=(单选按钮)findViewById(R.id.answerBtn2);
answerBtn3=(单选按钮)findViewById(R.id.answerBtn3);
answerBtn4=(单选按钮)findViewById(R.id.answerBtn4);
button2=(按钮)findViewById(R.id.button2);
for(int i=0;iif (radioGroup.getCheckedRadioButtonId() == -1)
{
// no radio buttons are checked
// You must choose your answer before proceeding to the next question
}
else
{
// one of the radio buttons is checked
}
radioButton.isChecked()