我是新手到Android,我的要求是选择一个单选按钮并转到Android中的相应活动? 谁能帮我? 在此先感谢.......
答案 0 :(得分:1)
这是给你的一个例子。这是带有两个分组radiobuttons的xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RadioButton
android:id="@+id/radiobutton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="radio 1"
/>
<RadioButton
android:id="@+id/radiobutton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="radio 2"/>
</RadioGroup> </LinearLayout>
在onCreate for your activity中,将setContentView设置为上面的视图后,找到radiobuttons并设置已检查的侦听器。
RadioButton buttonOne = (RadioButton)findViewById(R.id.radiobutton1);
buttonOne.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
//Go to the activity for button 1 here
MainActivity.this.startActivity(new Intent(MainActivity.this, ActivityYouWantToGoTo.class));
}
}
});
RadioButton buttonTwo = (RadioButton)findViewById(R.id.radiobutton2);
buttonTwo.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
//Go to the activity for button 2 here
MainActivity.this.startActivity(new Intent(MainActivity.this, ActivityYouWantToGoTo.class));
}
}
});
}
答案 1 :(得分:0)
在这两个指南中解释了您所需要的所有内容: