3排radiobutton分别用3个radiogroup包含:
布局如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:orientation="horizontal"
android:visibility="visible"
>
<RadioGroup
android:id="@+id/rg_trip_type_group1"
android:layout_marginLeft="10dip"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
style="@style/RadioButtonStyles"
android:textSize="12sp" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
style="@style/RadioButtonStyles"
android:textSize="12sp"
/>
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
style="@style/RadioButtonStyles"
android:textSize="12sp"
/>
</RadioGroup>
<RadioGroup
android:id="@+id/rg_trip_type_group2"
android:layout_marginLeft="20dip"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
style="@style/RadioButtonStyles"
android:textSize="12sp" />
<RadioButton
android:id="@+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
style="@style/RadioButtonStyles"
android:textSize="12sp"
/>
<RadioButton
android:id="@+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
style="@style/RadioButtonStyles"
android:textSize="12sp"
/>
</RadioGroup>
<RadioGroup
android:id="@+id/rg_trip_type_group3"
android:layout_marginLeft="20dip"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
style="@style/RadioButtonStyles"
android:textSize="12sp" />
<RadioButton
android:id="@+id/radioButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
style="@style/RadioButtonStyles"
android:textSize="12sp"
/>
<RadioButton
android:id="@+id/radioButton9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
style="@style/RadioButtonStyles"
android:textSize="12sp"
/>
</RadioGroup>
</LinearLayout>
</RelativeLayout>
activity代码如下:
package com.example.exradiobuttondemo;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity {
RadioGroup rg,rg1,rg2;// 3个 radiogroup
RadioButton rbtn_type1,rbtn_type2,rbtn_type3,rbtn_type4,rbtn_type5,
rbtn_type6,rbtn_type7,rbtn_type8,rbtn_type9;// 9个radiobutton
String str="raido文本内容";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpView();
}
private void setUpView() {
// TODO Auto-generated method stub
// radiogroup
rg=(RadioGroup) findViewById(R.id.rg_trip_type_group1);
rg1=(RadioGroup) findViewById(R.id.rg_trip_type_group2);
rg2=(RadioGroup) findViewById(R.id.rg_trip_type_group3);
//radiobutton
rbtn_type1=(RadioButton) findViewById(R.id.radioButton1);
rbtn_type2=(RadioButton) findViewById(R.id.radioButton2);
rbtn_type3=(RadioButton) findViewById(R.id.radioButton3);
rbtn_type4=(RadioButton) findViewById(R.id.radioButton4);
rbtn_type5=(RadioButton) findViewById(R.id.radioButton5);
rbtn_type6=(RadioButton) findViewById(R.id.radioButton6);
rbtn_type7=(RadioButton) findViewById(R.id.radioButton7);
rbtn_type8=(RadioButton) findViewById(R.id.radioButton8);
rbtn_type9=(RadioButton) findViewById(R.id.radioButton9);
allListener();
}
private void allListener() {
// TODO Auto-generated method stub
//radiogroup-1监听事件
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (group.getCheckedRadioButtonId()) {
case R.id.radioButton1:
rg1.clearCheck();
rg2.clearCheck();
group.check(R.id.radioButton1);
str=(String) rbtn_type1.getText();
break;
case R.id.radioButton2:
rg1.clearCheck();
rg2.clearCheck();
group.check(R.id.radioButton2);
str=(String) rbtn_type2.getText();
break;
case R.id.radioButton3:
rg1.clearCheck();
rg2.clearCheck();
group.check(R.id.radioButton3);
str=(String) rbtn_type3.getText();
break;
default:
break;
}
}
});
//radiogroup-2监听事件
rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (group.getCheckedRadioButtonId()) {
case R.id.radioButton4:
rg.clearCheck();
rg2.clearCheck();
group.check(R.id.radioButton4);
str=(String) rbtn_type4.getText();
break;
case R.id.radioButton5:
rg.clearCheck();
rg2.clearCheck();
group.check(R.id.radioButton5);
str= rbtn_type5.getText().toString().trim();
break;
case R.id.radioButton6:
rg.clearCheck();
rg2.clearCheck();
group.check(R.id.radioButton6);
str= rbtn_type6.getText().toString().trim();
break;
default:
break;
}
}
});
//radiogroup-3监听事件
rg2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (group.getCheckedRadioButtonId()) {
case R.id.radioButton7:
rg.clearCheck();
rg1.clearCheck();
group.check(R.id.radioButton7);
str= rbtn_type7.getText().toString().trim();
break;
case R.id.radioButton8:
rg.clearCheck();
rg1.clearCheck();
group.check(R.id.radioButton8);
str= rbtn_type8.getText().toString().trim();
break;
case R.id.radioButton9:
rg.clearCheck();
rg1.clearCheck();
group.check(R.id.radioButton9);
str= rbtn_type9.getText().toString().trim();
break;
default:
break;
}
}
});
}
@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;
}
}
布局如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:orientation="horizontal"
android:visibility="visible"
>
<RadioGroup
android:id="@+id/rg_trip_type_group1"
android:layout_marginLeft="10dip"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
style="@style/RadioButtonStyles"
android:textSize="12sp" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
style="@style/RadioButtonStyles"
android:textSize="12sp"
/>
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
style="@style/RadioButtonStyles"
android:textSize="12sp"
/>
</RadioGroup>
<RadioGroup
android:id="@+id/rg_trip_type_group2"
android:layout_marginLeft="20dip"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
style="@style/RadioButtonStyles"
android:textSize="12sp" />
<RadioButton
android:id="@+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
style="@style/RadioButtonStyles"
android:textSize="12sp"
/>
<RadioButton
android:id="@+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
style="@style/RadioButtonStyles"
android:textSize="12sp"
/>
</RadioGroup>
<RadioGroup
android:id="@+id/rg_trip_type_group3"
android:layout_marginLeft="20dip"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
style="@style/RadioButtonStyles"
android:textSize="12sp" />
<RadioButton
android:id="@+id/radioButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
style="@style/RadioButtonStyles"
android:textSize="12sp"
/>
<RadioButton
android:id="@+id/radioButton9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
style="@style/RadioButtonStyles"
android:textSize="12sp"
/>
</RadioGroup>
</LinearLayout>
</RelativeLayout>
activity代码如下:
package com.example.exradiobuttondemo;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity {
RadioGroup rg,rg1,rg2;// 3个 radiogroup
RadioButton rbtn_type1,rbtn_type2,rbtn_type3,rbtn_type4,rbtn_type5,
rbtn_type6,rbtn_type7,rbtn_type8,rbtn_type9;// 9个radiobutton
String str="raido文本内容";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpView();
}
private void setUpView() {
// TODO Auto-generated method stub
// radiogroup
rg=(RadioGroup) findViewById(R.id.rg_trip_type_group1);
rg1=(RadioGroup) findViewById(R.id.rg_trip_type_group2);
rg2=(RadioGroup) findViewById(R.id.rg_trip_type_group3);
//radiobutton
rbtn_type1=(RadioButton) findViewById(R.id.radioButton1);
rbtn_type2=(RadioButton) findViewById(R.id.radioButton2);
rbtn_type3=(RadioButton) findViewById(R.id.radioButton3);
rbtn_type4=(RadioButton) findViewById(R.id.radioButton4);
rbtn_type5=(RadioButton) findViewById(R.id.radioButton5);
rbtn_type6=(RadioButton) findViewById(R.id.radioButton6);
rbtn_type7=(RadioButton) findViewById(R.id.radioButton7);
rbtn_type8=(RadioButton) findViewById(R.id.radioButton8);
rbtn_type9=(RadioButton) findViewById(R.id.radioButton9);
allListener();
}
private void allListener() {
// TODO Auto-generated method stub
//radiogroup-1监听事件
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (group.getCheckedRadioButtonId()) {
case R.id.radioButton1:
rg1.clearCheck();
rg2.clearCheck();
group.check(R.id.radioButton1);
str=(String) rbtn_type1.getText();
break;
case R.id.radioButton2:
rg1.clearCheck();
rg2.clearCheck();
group.check(R.id.radioButton2);
str=(String) rbtn_type2.getText();
break;
case R.id.radioButton3:
rg1.clearCheck();
rg2.clearCheck();
group.check(R.id.radioButton3);
str=(String) rbtn_type3.getText();
break;
default:
break;
}
}
});
//radiogroup-2监听事件
rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (group.getCheckedRadioButtonId()) {
case R.id.radioButton4:
rg.clearCheck();
rg2.clearCheck();
group.check(R.id.radioButton4);
str=(String) rbtn_type4.getText();
break;
case R.id.radioButton5:
rg.clearCheck();
rg2.clearCheck();
group.check(R.id.radioButton5);
str= rbtn_type5.getText().toString().trim();
break;
case R.id.radioButton6:
rg.clearCheck();
rg2.clearCheck();
group.check(R.id.radioButton6);
str= rbtn_type6.getText().toString().trim();
break;
default:
break;
}
}
});
//radiogroup-3监听事件
rg2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (group.getCheckedRadioButtonId()) {
case R.id.radioButton7:
rg.clearCheck();
rg1.clearCheck();
group.check(R.id.radioButton7);
str= rbtn_type7.getText().toString().trim();
break;
case R.id.radioButton8:
rg.clearCheck();
rg1.clearCheck();
group.check(R.id.radioButton8);
str= rbtn_type8.getText().toString().trim();
break;
case R.id.radioButton9:
rg.clearCheck();
rg1.clearCheck();
group.check(R.id.radioButton9);
str= rbtn_type9.getText().toString().trim();
break;
default:
break;
}
}
});
}
@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;
}
}
来源:oschina
链接:https://my.oschina.net/u/1402721/blog/182653