向左向右(RadioGroup组与onCheckedChanged)

向左向右(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;

 

              }

           }

       });

    }

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值