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

时间:2014-03-07 06:05:07

标签: java android button onclick onclicklistener

我正在开发一个Android应用程序,其中问卷调查活动包含问题,其中包括单选按钮和按钮(下一步)。因此,当按下按钮时,我要检查所有问题是否都已被回答。如果有的话问题没有得到解答然后会弹出警告消息,说明特定问题没有得到解答。任何人都可以帮我解决java代码。 提前致谢。 Here is the view of my Questionnaire activity

这是Java代码。我已经评论了我收到错误的行。

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");      
 } 

4 个答案:

答案 0 :(得分:0)

您可以通过以下方式获取特定id中已选中RadioButton的{​​{1}}

RadioGroup

答案 1 :(得分:0)

您可以尝试以下内容。

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;
        }
}

如果要显示“警告”对话框而不是“吐司”,则可以将其替换为警告对话框。与rg2rg3相同,您可以查看。

答案 2 :(得分:0)

你必须在屏幕上有多个无线电组。

以下是检查是否在您的案例中尝试所有问题的示例

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
}

}

答案 3 :(得分:0)

您也可以使用以下代码:

       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();


                }

其中mBtn1,mBtn2 ..是你的radioButton的