单选按钮选择决定下一页

时间:2013-01-31 09:28:14

标签: java android radio-button

我是Android编程新手。我正在尝试编写代码,以便用户从两个单选按钮中进行选择,然后单击“下一步”按钮。然后,下一个按钮将用户根据选择的单选按钮将其带到两个页面中的一个。

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/Yes"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Yes" />

    <RadioButton
        android:id="@+id/No"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="No" />
</RadioGroup>

<Button
    android:id="@+id/Next"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Next" />

-

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainpage);
    }

我环顾四周,发现没有人回答这个问题。 任何帮助将不胜感激。提前谢谢。

4 个答案:

答案 0 :(得分:1)

在onClick()方法中,检查单击哪个RadioButton。

RadioButton rbtnYes = (RadioButton)findViewById(R.id.Yes);
RadioButton rbtnNo = (RadioButton)findViewById(R.id.No);
public void onClick(View v)
{
    if(rbtnYes.isChecked())
{
//Navigate to your firstpage
}
else
{
//Navigate to your second page
}
}

答案 1 :(得分:1)

RadioButton radioYes = (RadioButton)findViewById(R.id.Yes);
RadioButton radioNo = (RadioButton)findViewById(R.id.No);

//在Button onClick()

if(radioYes.isChecked()){
    Intent intent = new Intent(CurrentActivity.this, PageOne.class);
    CurrentActivity.this.startActivity(intent);
}else if(radioNo.isChecked()){
    Intent intent = new Intent(CurrentActivity.this, PageTwo.class);
    CurrentActivity.this.startActivity(intent);
}else{
    Toast.makeText(getApplicationContext(), "Select the radio button",Toast.LENGTH_LONG).show();
}

希望这会有所帮助..

答案 2 :(得分:0)

尝试类似的东西

private RadioButton button1 = null;
private RadioButton button2 = null;
onCreate()中的

button1 = (RadioButton) findViewById(R.id.rbtnButton1);
button2 = (RadioButton) findViewById(R.id.rbtnButton2);

在next的onClicklistener中:

if(button1.isChecked()) {
   // intent to page 1
} else if(button2.isChecked()) {
   // intent to page2
}

另一种方法是(仍然在下一个的onClickListener中):

RadioGroup g = (RadioGroup) findViewById(R.id.YOURRADIOGROUPID);
Intent i; 
switch (g.getCheckedRadioButtonId())
{
case R.id.BUTTON1ID
 //intent page 1
i= new Intent(this, ActivityForButton1.class);
break;

case R.id.BUTTON2ID
 //intent page 2
i= new Intent(this, ActivityForButton12.class);
break;
}
StarActivity(i);

答案 3 :(得分:0)

你可以试试这个

int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton)radioButtonGroup.findViewById(radioButtonID);

然后您可以检查选中了哪个按钮,然后您可以进行相应的导航。

相关问题