向左向右(RadioGroup组与onCheckedChanged)
新建一个继承Activity类的RadioGroupOnCheckedChangedActivity,并设置布局文件为:radiogrouponcheckchanged.xml。
首先在布局文件添加一个TextView和2个RadioGroup单选按钮
<TextView android:id="@+id/radiogrouponcheckchanged_tv01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/show_information" android:textSize="20sp" />
<RadioGroup android:id="@+id/radiogrouponcheckchanged_group" android:layout_width="fill_parent" android:layout_height="wrap_content" >
<RadioButton android:id="@+id/radiogrouponcheckchanged_rbtn01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/man" />
<RadioButton android:id="@+id/radiogrouponcheckchanged_rbtn02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/felman" /> </RadioGroup> |
而后在Activity代码中,点击RadiogGroup后将文字显示到TextView中。
package lyx.feng.second; .......
public class RadioGroupOnCheckedChangedActivity extends Activity { private TextView tv = null; private RadioGroup group = null; private RadioButton radioButton01 = null; private RadioButton radioButton02 = null;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.radiogrouponcheckchanged); this.tv = (TextView) super .findViewById(R.id.radiogrouponcheckchanged_tv01); this.group = (RadioGroup) super .findViewById(R.id.radiogrouponcheckchanged_group); this.radioButton01 = (RadioButton) super .findViewById(R.id.radiogrouponcheckchanged_rbtn01); this.radioButton02 = (RadioButton) super .findViewById(R.id.radiogrouponcheckchanged_rbtn02); this.group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.radiogrouponcheckchanged_rbtn01: tv.setText(radioButton01.getText()); break; case R.id.radiogrouponcheckchanged_rbtn02: tv.setText(radioButton02.getText()); break;
} } }); } }
|