简体   繁体   English

如何验证Android中的单选按钮?

[英]How to validate radio button in android?

This is my coding. 这是我的编码。 I don't know how to validate the radio button. 我不知道如何验证单选按钮。 When I click the next button,it still can proceed eventhough the radio button is not clicked. 当我单击下一个按钮时,即使未单击单选按钮,它仍然可以继续进行。 Sorry for my bad English.Please help me. 对不起,我的英语不好。请帮帮我。 Thank you. 谢谢。

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dengue_ques2);

    radioGroup = (RadioGroup) findViewById(R.id.radioGroupDengue2);
    yes2 = (RadioButton) findViewById(R.id.radioButtonQues2); //the answer is yes
    no2 = (RadioButton) findViewById(R.id.radioButtonQues2No); //the answer is no

    Next=(Button) findViewById(R.id.buttonNextQues2); 

    yes2.setSelected(true);
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            if (i == R.id.radioButtonQues2)
            {
                selectedType = yes2.getText().toString();
            }
            else if (i == R.id.radioButtonQues2No)
            {
                selectedType = no2.getText().toString();
            }
            /* this part does not work
            else if (i == R.id.radioButtonQues2.isChecked(false) && 
                     i ==  R.id.radioButtonQues2No.isChecked(false))
            {
             Toast.makeText(this, "Please choose 1 answer", 
                                   Toast.LENGTH_SHORT).show(); 
            }*/
        }
    });

    Next.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent = new Intent(dengueQues2.this, dengueQues3.class);
            intent.putExtra("REST2", selectedType);
            startActivity(intent);
        }
    });
}

    public void onClickYes2 (View view)
    {
        Toast.makeText(this, "You have select 1 to 3 days.",Toast.LENGTH_SHORT).show();
    }

    public void onClickNo2 (View view)
    {
        Toast.makeText(this, "You have select 3 to 14 days.", Toast.LENGTH_SHORT).show();
    }

You need to use the setChecked() method not the setSelected() method on the radio button. 您需要使用单选按钮上的setChecked()方法而不是setSelected()方法。

Also you need to default selectedType = yes2.getText().toString(); 另外,您还需要默认selectedType = yes2.getText().toString(); since you are setting that as the default selection in onCreate() . 因为您将其设置为onCreate()的默认选择。

yes2.setChecked(true);
selectedType = yes2.getText().toString();

EDIT showing how to use no default selection: 编辑显示如何不使用默认选择:

Next.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(yes2.isChecked() || no2.isChecked()) {
            Intent intent = new Intent(dengueQues2.this, dengueQues3.class);
            intent.putExtra("REST2", selectedType);
            startActivity(intent);
        } else {
            Toast.makeText(this, "Please choose 1 answer", 
                          Toast.LENGTH_SHORT).show();
        }
    }
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM