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

}

}

4

5 回答 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 之外的任何值,则您有一个选定的按钮。

于 2012-12-19T06:39:36.303 回答
3

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

真实条件:因为您要确保必须选择选项:

boolean checked = ((RadioButton) view).isChecked();
于 2012-12-19T06:38:03.917 回答
3

您正在检查 Next 按钮本身的选择状态。但是您想检查单选按钮选择状态。所以你必须给它:

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();
        }
于 2012-12-19T06:38:57.293 回答
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
}
于 2012-12-19T06:54:26.403 回答
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>
于 2020-01-10T18:44:05.873 回答