Android : Help in RadioButton Toast

落花浮王杯 提交于 2019-12-12 12:18:13

问题


If user clicks NEXT button without selecting option it has to toast a message, "pls select any one" else it should goto next screen. I have tried but its not going to next screen, Instead its showing toast"pls select any one" and my code

public class Question1 extends Activity implements OnClickListener {
Button vid, next;
Typeface tp;
TextView tv;
RadioGroup rg;
RadioButton rb;
Button b;
SharedPreferences sp;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.question1);
    sp = getSharedPreferences("AppFile1", 0);

    tv = (TextView) findViewById(R.id.tvQuestion1);

    rg = (RadioGroup) findViewById(R.id.radioGroup1);
    vid = (Button) findViewById(R.id.buttonQuestion1VIdeo);
    next = (Button) findViewById(R.id.buttonQuestion1Next);
    vid.setOnClickListener(this);
    next.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.buttonQuestion1VIdeo:
        Intent i = new Intent(Question1.this, VideoActivity.class);
        startActivity(i);
        break;
    case R.id.buttonQuestion1Next:
        if (next.isSelected()) {
            int selectedId = rg.getCheckedRadioButtonId();
            rb = (RadioButton) findViewById(selectedId);
            String s = rb.getText().toString();
            SharedPreferences.Editor ed = sp.edit();
            ed.putString("q1", s);
            ed.commit();
            Intent ia = new Intent(Question1.this, Question2.class);
            startActivity(ia);

        } else {
            Toast.makeText(getApplicationContext(), "Please Select Answer", 0).show();
        }

        break;
    default:
        break;
    }

}

}


回答1:


Try with:

if (rg.getCheckedRadioButtonId()!=-1) {
        int selectedId = rg.getCheckedRadioButtonId();
        rb = (RadioButton) findViewById(selectedId);
        String s = rb.getText().toString();
        SharedPreferences.Editor ed = sp.edit();
        ed.putString("q1", s);
        ed.commit();
        Intent ia = new Intent(Question1.this, Question2.class);
        startActivity(ia);

    } else {
        Toast.makeText(getApplicationContext(), "Please Select Answer", 0).show();
    }

The documentation says that if no button is checked, getCheckedRadioButtonId() returns -1.

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

Therefore, if you have any value except -1 from that function, you have a selected button.




回答2:


wrong condition: if (next.isSelected())

True condition: As you want to make sure option must be select:

boolean checked = ((RadioButton) view).isChecked();



回答3:


You are checking the selection state of Next button itself in condition. But you want to check radio button selection state. So you have to give it as:

if (rg.getCheckedRadioButtonId()!=-1) {
            int selectedId = rg.getCheckedRadioButtonId();
            rb = (RadioButton) findViewById(selectedId);
            String s = rb.getText().toString();
            SharedPreferences.Editor ed = sp.edit();
            ed.putString("q1", s);
            ed.commit();
            Intent ia = new Intent(Question1.this, Question2.class);
            startActivity(ia);

        } else {
            Toast.makeText(getApplicationContext(), "Please Select Answer", 0).show();
        }



回答4:


the condition will be like this

rb1  = (RadioButton)findViewById(R.id.rb1);
rb2  = (RadioButton)findViewById(R.id.rb2);
rb3  = (RadioButton)findViewById(R.id.rb3);

if(rb1.isChecked()==false && rb2.isChecked()==false && rb3.isChecked()==false)
{
     // Toast message is here
}
else
{
    // Intent code is here
}


来源:https://stackoverflow.com/questions/13946800/android-help-in-radiobutton-toast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!