簡體   English   中英

在“按鈕”上單擊以檢查是否選擇了單選按鈕

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

我正在開發一個Android應用程序,其中``問卷調查''活動包含作為單選按鈕和一個Button(Next)的問題。所以當按下按鈕時,我必須檢查是否所有問題都已得到回答。如果未回答,則應彈出一條警告消息,指出未回答特定的問題號。任何人都可以通過Java代碼幫助我。 提前致謝。 這是我的問卷調查活動的視圖

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

您可以通過以下方式獲取特定RadioGroup選中的RadioButtonid

int selectedId = radioGroup.getCheckedRadioButtonId();

您可以嘗試以下類似方法。

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一樣,您可以檢查。

您必須在屏幕上具有多個無線電組。

這是檢查您是否遇到所有問題的示例

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
}

}

您還可以使用以下代碼:

       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的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM