Java 单选按钮选择决定下一页
我是android编程新手。我试图编写代码,以便用户从两个单选按钮中进行选择,然后单击“下一步”按钮。然后,“下一步”按钮将用户带到两个页面中的一个页面,这两个页面是根据所选择的单选按钮选择的Java 单选按钮选择决定下一页,java,android,radio-button,Java,Android,Radio Button,我是android编程新手。我试图编写代码,以便用户从两个单选按钮中进行选择,然后单击“下一步”按钮。然后,“下一步”按钮将用户带到两个页面中的一个页面,这两个页面是根据所选择的单选按钮选择的 <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/Yes"
<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" />
我环顾四周,没有找到回答这个问题的答案。
任何帮助都将不胜感激。提前感谢。在onClick()方法中,检查单击了哪个单选按钮,如下所示
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
}
}
试试那样的
private RadioButton button1 = null;
private RadioButton button2 = null;
在onCreate()中:
在接下来的onClicklistener中:
if(button1.isChecked()) {
// intent to page 1
} else if(button2.isChecked()) {
// intent to page2
}
另一种方法是(仍在next的onClickListener中):
你可以试试这个
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton)radioButtonGroup.findViewById(radioButtonID);
然后你可以检查哪个按钮被选中,你可以相应地导航
RadioButton radioYes = (RadioButton)findViewById(R.id.Yes);
RadioButton radioNo = (RadioButton)findViewById(R.id.No);
//在按钮onClick()中
希望这有帮助
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);
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton)radioButtonGroup.findViewById(radioButtonID);
RadioButton radioYes = (RadioButton)findViewById(R.id.Yes);
RadioButton radioNo = (RadioButton)findViewById(R.id.No);
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();
}