Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何正确使用;如果/否则如果“;Android中的单选按钮_Java_Android - Fatal编程技术网

Java 如何正确使用;如果/否则如果“;Android中的单选按钮

Java 如何正确使用;如果/否则如果“;Android中的单选按钮,java,android,Java,Android,我试着用单选按钮和if语句简单地检查一下,选择题的答案是否正确 我想把它作为教学应用程序的一部分,但是我可能误解了 int radioId = radioGroup.getCheckedRadioButtonId() 我希望你们能帮助我。我在一个单选组中使用了四个单选按钮,并在下面发布了xml和java package com.example.myapplication; 导入androidx.appcompat.app.appcompat活动; 导入android.os.Bundle;

我试着用单选按钮和if语句简单地检查一下,选择题的答案是否正确

我想把它作为教学应用程序的一部分,但是我可能误解了

int radioId = radioGroup.getCheckedRadioButtonId()
我希望你们能帮助我。我在一个单选组中使用了四个单选按钮,并在下面发布了xml和java


package com.example.myapplication;
导入androidx.appcompat.app.appcompat活动;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.RadioButton;
导入android.widget.RadioGroup;
导入android.widget.TextView;
导入android.widget.Button;
导入android.widget.Toast;
公共类IntroQuiz1扩展了AppCompative活动{
放射组放射组;
单选按钮单选按钮;
文本视图文本视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro_quiz1);
放射组=findViewById(R.id.radioGroup);
textView=findViewById(R.id.textView2);
按钮纽扣=findViewById(R.id.按钮检查);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
int radioId=radioGroup.getCheckedRadioButtonId();
int检验=1;
radioButton=findViewById(radioId);
字符串检查字符串=(字符串)radioButton.getText();
如果(检查字符串==“RadioButton1”){
setText(“Korrekt”);
}否则{
setText(检查字符串);
}
}
});
}
公共作废检查按钮(视图v){
int radioId=radioGroup.getCheckedRadioButtonId();
radioButton=findViewById(radioId);
Toast.makeText(此“选定单选按钮:”+radioButton.getText(),
吐司。长度(短)。show();
}
}
然而,我没有得到“Korrekt”打印任何单选按钮检查。这已经是一个解决办法。我不想比较字符串。我更愿意使用Id,但当我试图使用radiod来检查正确答案时,radiod似乎不是一个整数,正如我迄今所学的那样,是正确的吗?
所以,我有点困惑,有人能帮我纠正这个代码并告诉我我做错了什么吗?

你应该将你的id与其他id进行比较,而不是字符串; 比如:


您应该将您选择的id与其他id进行比较,而不是字符串; 比如:


始终使用equals()方法比较java中的字符串。在您的情况下,这将是“RadioButton1”.equals(检查字符串)。查看@Guy提供的问题了解更多信息。谢谢大家,很高兴知道。然而,比较字符串只是一种变通方法,在这种情况下我根本不想使用它。总是使用equals()方法在java中比较字符串。在您的情况下,这将是“RadioButton1”.equals(检查字符串)。查看@Guy提供的问题了解更多信息。谢谢大家,很高兴知道。然而,比较字符串只是一种变通方法,我根本不想在这种情况下使用。非常感谢,这很有效!但是,如果我想允许多个正确答案,我不能使用它。这就是我想使用if语句的原因。为什么
if(R.id.radioButton1){textView.setText(“Korrekt”);}else{textView.setText(“Falsch”);}
也不起作用?它给了我“错误:不兼容类型:int不能转换为布尔值”R.id.radioButton是int,如果您想要多个正确的,您必须使用复选框not RadioGroup。@celdrion只需使用一个fall-through
switch
语句(这样可以有一组匹配的id)
R.id.radioButton1
是一个
int
而不是
boolean
值-除此之外,如果
语句与此答案完全无关。好的,非常感谢。我会看看复选框!但我还是不明白为什么我不能从放射性d中得到int。我试图打印它,但我总是得到“假”。不管怎样,你们帮了我很多!非常感谢,效果很好!但是,如果我想允许多个正确答案,我不能使用它。这就是我想使用if语句的原因。为什么
if(R.id.radioButton1){textView.setText(“Korrekt”);}else{textView.setText(“Falsch”);}
也不起作用?它给了我“错误:不兼容类型:int不能转换为布尔值”R.id.radioButton是int,如果您想要多个正确的,您必须使用复选框not RadioGroup。@celdrion只需使用一个fall-through
switch
语句(这样可以有一组匹配的id)
R.id.radioButton1
是一个
int
而不是
boolean
值-除此之外,如果
语句与此答案完全无关。好的,非常感谢。我会看看复选框!但我还是不明白为什么我不能从放射性d中得到int。我试图打印它,但我总是得到“假”。不管怎样,你们帮了我很多!
 int radioId = radioGroup.getCheckedRadioButtonId();
    switch(radioId ){
        case R.id.radioButton1:
            //your code;
            break;
        case R.id.radioButton2:
            //your code;
            break;
        case R.id.radioButton3:
            //your code;
            break;
    }