2014-03-07 164 views
1

我正在开发一个android应用程序,其中问卷活动包含的问题是单选按钮和一个按钮(Next)。因此,当按下按钮时,我必须检查是否所有的问题回答。如果任何问题没有回答,然后应该弹出一个警告消息,说明具体的问题没有回答。可以任何人请帮助我的Java代码。 在此先感谢。 Here is the view of my Questionnaire activityOn Button单击以检查单选按钮是否被选中

这是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");  
} 
+0

所以,你的问题是,你正在** **方法'OnClick()'**? –

+0

一旦看到https://github.com/robhinds/AndroidChuckQuiz/blob/master/src/com/tmm/android/chuck/QuestionActivity.java –

+0

是的哈米德沙图,有问题。 – Steve

回答

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..are您radioButton的

+0

它不是强制使用radiogroup。我已经使用了单选按钮 – jyomin

+0

jyomin感谢您的代码。它的工作部分意味着如果任何问题没有得到解答,并且如果按下按钮,它不会弹出警告消息,好像所有问题都回答了按钮被按下了导航到下一个活动。 – Steve

+0

检查我编辑过的帖子.. – jyomin

0

你可以得到检查RadioButtonid特定RadioGroup

int selectedId = radioGroup.getCheckedRadioButtonId(); 
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; 
     } 
} 

如果您想要显示Alert对话框而不是Toast,那么您可以用警报对话框替换它。就像你的rg2rg3明智的一样,你可以检查。

0

你必须在屏幕上有多个radiogroups。

这里是检查任何所有的问题是否在你的情况下试图与否的例子

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 
} 

} 
相关问题