您的位置:首页 > 移动开发 > Android开发

Android程序:RadioGroup的用法(多选一)

2015-03-26 17:54 447 查看
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/radioGroup">

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb1"
android:text="男"
android:checked="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb2"
android:text="女"/>

</RadioGroup>


public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener{
private RadioGroup radioGroup;

@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(this);

}

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb1:
Toast.makeText(MainActivity.this,"you are a boy",Toast.LENGTH_SHORT).show();
break;
case R.id.rb2:
Toast.makeText(MainActivity.this,"you are a girl",Toast.LENGTH_SHORT).show();
break;
default:
break;
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  程序 radiogroup android