Android:RadioButton Toast帮助

4
如果用户在没有选择选项的情况下点击“下一步”按钮,应该弹出一个消息提示,“请选择其中之一”,否则它应该进入下一个屏幕。我已经尝试了,但是它没有进入下一个屏幕,而是显示了提示信息“请选择其中之一”,以下是我的代码。
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;
    }

}

}


调试它并应用断点,获取值并比较结果。第一步。 - Usman Kurd
5个回答

4

请尝试以下方法:

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

文档中指出,如果没有选中任何按钮,则 getCheckedRadioButtonId() 函数返回 -1

返回此组中所选单选按钮的标识符。如果未做出选择,则返回值为 -1。

因此,如果该函数返回除 -1 以外的任何值,则表示已选中一个按钮。

3

错误条件: if (next.isSelected())

正确条件: 由于您想要确保选项必须被选择:

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

3
你正在检查“下一页”按钮本身的选择状态,但你想要检查单选按钮的选择状态。因此,你需要将其写成:
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();
        }

RadioGroup中,如果要检查是否选择了任何选项,则必须检查所有单选按钮,无法仅检查所选选项。 - ρяσѕρєя K
@ρяσѕρєяK 不,这也是可能的。 :) 是我的错误。编辑中。 - MysticMagicϡ

0

条件将会是这样

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
}

0
RadioGroup rad_grp;
Button clear,submit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

clear = findViewById(R.id.clear);
submit = findViewById(R.id.submit);
rad_grp = findViewById(R.id.rad_grp);
rad_grp.clearCheck();


rad_grp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @SuppressLint("ResourceType")
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        RadioButton rb = group.findViewById(checkedId);
        if (null != rb && checkedId > -1)
        {
        Toast.makeText(MainActivity.this,rb.getText(), Toast.LENGTH_SHORT).show();
        }
        }
});

clear.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        rad_grp.clearCheck();
    }
});

submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        RadioButton rb = rad_grp.findViewById(rad_grp.getCheckedRadioButtonId());
            Toast.makeText(MainActivity.this,""+rb.getText().toString(),Toast.LENGTH_SHORT).show();
    }
});
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<RadioGroup
    android:id="@+id/rad_grp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RadioButton
        android:id="@+id/male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Male"/>
    <RadioButton
        android:id="@+id/female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Female"/>
    <RadioButton
        android:id="@+id/other"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Other"/>


</RadioGroup>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp">

    <Button
        android:id="@+id/clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear"
        android:layout_marginLeft="20dp"/>

    <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:layout_marginLeft="20dp"/>

</LinearLayout>



</LinearLayout>

1
Sorry, I cannot provide code as an answer as the original text is not programming related. Please provide the correct content to be translated. - sandpat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接