我希望我的复选框仅显示2个选项的正确答案,但如果选择2个以上的选项(包括正确的选项),则会显示正确的答案,这不是所需的结果。
package com.example.vidit.project3;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.Toast;
public class question2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question2);
}
public void submit2(View v)
{
CheckBox cb1= (CheckBox) findViewById(R.id.c1);
CheckBox cb2=(CheckBox) findViewById(R.id.c2);
CheckBox cb3=(CheckBox) findViewById(R.id.c3);
CheckBox cb4=(CheckBox) findViewById(R.id.c4);
//Show Correct option for only one combination
if(cb1.isChecked() && cb2.isChecked())
{
Toast toast = Toast.makeText(getApplicationContext(),"Whopiee! Correct Answer",Toast.LENGTH_LONG);
toast.show();
}
else if(cb1.isChecked() && cb2.isChecked() && cb3.isChecked() && cb4.isChecked())
{
Toast toast = Toast.makeText(getApplicationContext(),"Arghh! Incorrect Answer",Toast.LENGTH_LONG);
toast.show();
}
else if(cb1.isChecked() && cb2.isChecked() && cb3.isChecked())
{
Toast toast = Toast.makeText(getApplicationContext(),"Arghh! Incorrect Answer",Toast.LENGTH_LONG);
toast.show();
}
else if(cb1.isChecked() && cb2.isChecked() && cb4.isChecked())
{
Toast toast = Toast.makeText(getApplicationContext(),"Arghh! Incorrect Answer",Toast.LENGTH_LONG);
toast.show();
}
else
{
Toast toast = Toast.makeText(getApplicationContext(),"Arghh! Incorrect Answer",Toast.LENGTH_LONG);
toast.show();
}
}
public void toQuestion3(View v)
{
Intent intent = new Intent(this,question3.class);
startActivity(intent);
}
}
您诚挚的, Vidit Shah
答案 0 :(得分:1)
像这样制作你的第一个if
:
if(cb1.isChecked() && cb2.isChecked() && !cb3.isChecked() && !cb4.isChecked()){
Toast toast = Toast.makeText(getApplicationContext(),"Whopiee! Correct Answer",Toast.LENGTH_LONG);
toast.show();
}
您可以通过以下方式汇总代码:
boolean correct = cb1.isChecked() && cb2.isChecked() && !cb3.isChecked() && !cb4.isChecked();
String message = correct ? "Whopiee! Correct Answer" : "Arghh! Incorrect Answer";
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
答案 1 :(得分:0)
if(cb1.isChecked() && cb2.isChecked() && !cb3.isChecked() && !cb4.isChecked())
{
Toast.makeText(getApplicationContext(),"Whopiee! Correct Answer",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(),"Arghh! Incorrect Answer",Toast.LENGTH_LONG).show();
}