首页 > 代码库 > 熟悉AndroidAPI系列3——RadioGroup and RaioButton
熟悉AndroidAPI系列3——RadioGroup and RaioButton
选择A,会同时选中A,C
选择B,会同时选中A,C
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RadioGroup android:id="@+id/radioGrp1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/aId" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "A"/> <RadioButton android:id="@+id/bId" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "B"/> </RadioGroup><RadioGroup android:id="@+id/radioGrp2" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/cId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text = "C" /> <RadioButton android:id="@+id/dId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="D" /> </RadioGroup> </LinearLayout>
package com.njulya.testradiobutton;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;public class MainActivity extends Activity { private RadioGroup radioGrp1; private RadioGroup radioGrp2; private RadioButton aButton; private RadioButton bButton; private RadioButton cButton; private RadioButton dButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_1); radioGrp1 = (RadioGroup)findViewById(R.id.radioGrp1); radioGrp2 = (RadioGroup)findViewById(R.id.radioGrp2); aButton = (RadioButton)findViewById(R.id.aId); bButton = (RadioButton)findViewById(R.id.bId); cButton = (RadioButton)findViewById(R.id.cId); dButton = (RadioButton)findViewById(R.id.dId); RadioGroupListener rGrpListener = new RadioGroupListener(); radioGrp1.setOnCheckedChangeListener(rGrpListener); radioGrp2.setOnCheckedChangeListener(rGrpListener); } //import android.widget.RadioGroup.OnCheckedChangeListener; private class RadioGroupListener implements OnCheckedChangeListener{ @Override public void onCheckedChanged(RadioGroup radioGrp, int checkedId) { if(checkedId == aButton.getId()){ cButton.setChecked(true); }else if(checkedId == bButton.getId()){ dButton.setChecked(true); }else if(checkedId == cButton.getId()){ aButton.setChecked(true); }else if(checkedId == dButton.getId()){ bButton.setChecked(true); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
熟悉AndroidAPI系列3——RadioGroup and RaioButton
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。