运行时的RadioGroup可见性问题

时间:2016-07-13 11:30:50

标签: java android if-statement android-activity android-radiogroup

我在RadioGroup的可见性方面写了一个小逻辑,但是当我尝试它时失败了:

if ("some condition") {
     showAlert("Some Message");
} else if ("some condition") {
     showAlert("Some Message");
} else if(radioGroup.getVisibility() == View.VISIBLE) {

     if (radioGroup.getCheckedRadioButtonId() == -1) {

         Toast.makeText(getApplicationContext(), "Please make a selection", Toast.LENGTH_SHORT).show();
         return;
     }

 } else {

   saveData();
   finish();

}

还尝试了这个:

 if ("some condition") {
     showAlert("Some Message");
 } else if ("some condition") {
     showAlert("Some Message");   
 } else {

     if(radioGroup.getVisibility() == View.VISIBLE) {

     if (radioGroup.getCheckedRadioButtonId() == -1) {

         Toast.makeText(getApplicationContext(), "Please make a selection", Toast.LENGTH_SHORT).show();
         return;
     }

     else {

          saveData();
          finish();
      }

    }

    else {

       saveData();
       finish();
   }    

}

我上面尝试过的,

我只是想展示Toast"请做出选择"如果Radio Group是Visible并且我的上述代码中没有选择任何单选按钮,但我收到此消息"请进行选择"甚至 RadioGroup 在活动中都是not visible

我犯了哪些错误?为什么我要来"请做出选择"甚至RadioGroup在Activity中不可见?

1 个答案:

答案 0 :(得分:0)

试试这个,它可能会有所帮助

    if(radioGroup.getVisibility() == View.VISIBLE) {
    if (radioGroup.getCheckedRadioButtonId() == -1) {

    Toast.makeText(getApplicationContext(), "Please make a selection", Toast.LENGTH_SHORT).show();
    return;
    } else {
    saveData();
    finish();
    }
} else { 
    if ("some condition 1") {showAlert("Some Message 1");}
 else {showAlert("Some Message 2");}
}

祝你好运