如何选择一个单选按钮,并在android中进入该活动?

问题描述:

我是新来android,我的要求是选择一个单选按钮,并在Android中相应的活动? 任何人都可以帮助我吗? 在此先感谢.......如何选择一个单选按钮,并在android中进入该活动?

+0

你至少试过做过什么吗?我的意思是......你有复选框已编程?如果是这样,请你提供代码,以便我们提供帮助? – Cristian 2010-11-19 16:38:41

所有你需要的在那些两位导游解释:

下面是这样的一个例子您。这是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为您的活动,您的setContentView上述观点后,找到单选按钮和设置检查听众。

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