简体   繁体   English

在“按钮”上单击以检查是否选择了单选按钮

[英]On Button click to check whether the radio buttons are selected

I'm Developing an android app in which the Questionnaire activity contains Questions which as radio Buttons and also a Button(Next).So when the button is pressed I've to check whether all the Questions are answered.if any of the Question is not answered then a alert message should pop up stating that the particular Question no is not answered.Can anyone please help me with the java code. 我正在开发一个Android应用程序,其中``问卷调查''活动包含作为单选按钮和一个Button(Next)的问题。所以当按下按钮时,我必须检查是否所有问题都已得到回答。如果未回答,则应弹出一条警告消息,指出未回答特定的问题号。任何人都可以通过Java代码帮助我。 Thanks in Advance. 提前致谢。 这是我的问卷调查活动的视图

Here is the Java code. 这是Java代码。 I've commented on the line where I'm getting an error. 我在出现错误的行中发表了评论。

public class ManagerQuestionnaire1 extends Activity
{

 RadioButton rb1;
 RadioButton rb2;
 RadioButton rb3;
 RadioButton rb4;
 RadioButton rb5;
 RadioButton rb6;
 RadioButton rb7;
 RadioButton rb8;
 RadioButton rb9;
 RadioGroup rg1;
 RadioGroup rg2;
 RadioGroup rg3;
 Button next;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_manager_questionnaire1);
    addButtonListener();
    }
public void addButtonListener()
{
 rb1=(RadioButton)findViewById(R.id.radioButton1);
 rb2=(RadioButton)findViewById(R.id.radioButton2);
 rb3=(RadioButton)findViewById(R.id.radioButton3);
 rb4=(RadioButton)findViewById(R.id.radioButton4);
 rb5=(RadioButton)findViewById(R.id.radioButton5);
 rb6=(RadioButton)findViewById(R.id.radioButton6);
 rb7=(RadioButton)findViewById(R.id.radioButton7);
 rb8=(RadioButton)findViewById(R.id.radioButton8);
 rb9=(RadioButton)findViewById(R.id.radioButton9);
 rg1=(RadioGroup)findViewById(R.id.Mquestion1);
 rg2=(RadioGroup)findViewById(R.id.Mquestion2);
 rg3=(RadioGroup)findViewById(R.id.Mquestion3);
    Button next=(Button)findViewById(R.id.button1);
 next.setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(View v)
     {
         if(validationSuccess()){
             Intent intent = new Intent(ManagerQuestionnaire1.this, ManagerQuestionnaire2.class);
             startActivity(intent);
         }
     }
 });
}

 private Boolean validationSuccess()
 {
if(rg1.getCheckedRadioButtonId()==-1&&rg2.getCheckedRadioButtonId()==-1&&rg3.getCheckedRadioButtonId()==-1)
{
    alertDialog();
    return false;
}
 if(rb1.isChecked()==false&rb2.isChecked()==false&&rb3.isChecked()==false)
 {
     alertDialog();
     return false;
     }
 if(rb4.isChecked()==false&&rb5.isChecked()==false&&rb6.isChecked()==false)
 {
     alertDialog();
     return false;
     }
 if(rb7.isChecked()==false&&rb8.isChecked()==false&&rb9.isChecked()==false)
 {
     alertDialog();
     return false;
     } 
 return true;
  }
  private void alertDialog()
{
  AlertDialog alert= new AlertDialog.Builder(ManagerQuestionnaire1.this).create();
  alert.setTitle("Exception:Complete the Questions");
  alert.setMessage("Please ensure all Questions are answered");      
 } 

您可以通过以下方式获取特定RadioGroup选中的RadioButtonid

int selectedId = radioGroup.getCheckedRadioButtonId();

You can try something like below. 您可以尝试以下类似方法。

public void onClick(View v) {    

        int checked = rg1.getCheckedRadioButtonId();

        switch(checked)
        {
        case R.id.always:
        Toast.makeText(ManagerQuestionnaire1 .this, "First is selected", Toast.LENGTH_SHORT).show();
        break;
        case R.id.sometime:
        Toast.makeText(ManagerQuestionnaire1 .this, "Second is selected", Toast.LENGTH_SHORT).show();
        break;
        case R.id.notatall:
        Toast.makeText(ManagerQuestionnaire1 .this, "Third is selected", Toast.LENGTH_SHORT).show();
        break;
        default:
        Toast.makeText(ManagerQuestionnaire1 .this, "pleas check any button", Toast.LENGTH_SHORT).show();
        break;
        }
}

If you want to show Alert dialog instead of toast then you can replace it with alert dialog. 如果要显示警报对话框而不是吐司,则可以将其替换为警报对话框。 Like wise same for your rg2 and rg3 you can check. 与您的rg2rg3一样,您可以检查。

you must be having multiple radiogroups on the screen. 您必须在屏幕上具有多个无线电组。

Here is the example to check whether any all questions are attempted or not in your case 这是检查您是否遇到所有问题的示例

int radioGroupIds = new int []{R.id.rg1, R.id.rg2, r.id.rg3};

for(int rg : radioGroupIds)
{

int selectedAns = (RadioGroup)findViewById(rg).getCheckedRadioButtonId();

// Returns the identifier of the selected radio button in this group. Upon empty selection, the returned value is -1.

if(selectedAns == -1)
{
      // TODO answer is not selected
      // This represents that there is missing answer of any question
}else
{
      // TODO answer is selected
      // This represents that answer is selected for question
}

}

You may also use below code: 您还可以使用以下代码:

       next.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if(validationSuccess()){
                            Intent intent = new Intent(ManagerQuestionnaire1.this, ManagerQuestionnaire2.class);
                            startActivity(intent);
                        }
                    }
                });

        private Boolean validationSuccess(){
    if(rg1.getCheckedRadioButtonId()==-1&&rg2.getCheckedRadioButtonId()==-1&&rg3.getCheckedRadioButtonId()==-1){
    alertDialog();
    return false;
    }


//optional to add whether to check which questn is not answered   

        if(mBtn1.isChecked()==false&&mBtn2.isChecked()==false&&mBtn3.isChecked()==false){
                    alertDialog();
                    return false;
                    }
                if(mBtn4.isChecked()==false&&mBtn5.isChecked()==false&&mBtn6.isChecked()==false){
                    alertDialog();
                    return false;
                    }
                if(mBtn7.isChecked()==false&&mBtn8.isChecked()==false&&mBtn9.isChecked()==false){
                    alertDialog();
                    return false;
                    }
    return true;    
            }




    private void alertDialog() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            ManagerQuestionnaire1.this);
    alertDialogBuilder.setMessage("Please ensure all Questions are answered")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

        AlertDialog alert = alertDialogBuilder.create();
        alert.show();


                }

where mBtn1,mBtn2..are your radioButton's 其中mBtn1,mBtn2 ..是您的radioButton的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查单击按钮时是否选择了单选按钮? - How to check if radio buttons are selected on button click? 尝试检查单选按钮是否被选中 - Trying to check whether a radio button is checked or not 需要禁用按钮,具体取决于是否选择了单选按钮 - Need to disable buttons depending on if a radio button is selected 单击按钮上的“读取选定的单选按钮” - Reading Selected Radio Button on button click 如何检查每个无线电组中的按钮是否被选中? - How to check is the button is selected in each radio group? 如何检查单选按钮是否在 Selenium WebDriver 中被选中? - How to check if the radio button is selected or not in Selenium WebDriver? 布尔检查后单击单选按钮 - Click on Radio button after boolean check 单击重置按钮后,如何取消所有选中的复选框,取消选中单选按钮,清除标记的文本字段? - How can i unckeck all the selected check boxes, uncheck radio buttons, clear marked text fields after clicking reset button? Selenium Java,如何检查是否选择了多个单选按钮? - Selenium Java, How to check if multiple radio buttons are selected? 如何检查按下按钮时选中了哪些单选按钮 - How to check which radio buttons are checked upon pressing a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM