RadioGroup & RadioButton OnCheckedChangeListener

Develop Tool: IntelliJ IDEA

Coding:

src/com.hhy.RadioButton/MyActivity:

package com.hhy.RadioButton;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import org.w3c.dom.Text;

public class MyActivity extends Activity {

    private RadioGroup radioGroup01, radioGroup02;
    private RadioButton radioBtn01, radioBtn02, radioBtn03, radioBtn04;
    private TextView textView01, textView02, textView03, textView04;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        radioGroup01 = (RadioGroup)findViewById(R.id.radioGroup01);
        radioBtn01 = (RadioButton)findViewById(R.id.radioBtn01);
        radioBtn02 = (RadioButton)findViewById(R.id.radioBtn02);
        textView01 = (TextView)findViewById(R.id.textView01);
        textView02 = (TextView)findViewById(R.id.textView02);
        textView03 = (TextView)findViewById(R.id.textView03);
        textView04 = (TextView)findViewById(R.id.textView04);
        radioGroup02 = (RadioGroup)findViewById(R.id.radioGroup02);
        radioBtn03 = (RadioButton)findViewById(R.id.radioBtn03);
        radioBtn04 = (RadioButton)findViewById(R.id.radioBtn04);

        RadioBtnListener btnListener = new RadioBtnListener();
        radioBtn01.setOnCheckedChangeListener(btnListener);
        radioBtn03.setOnCheckedChangeListener(btnListener);

        RadioGroupListener listener = new RadioGroupListener();
        radioGroup01.setOnCheckedChangeListener(listener);
        radioGroup02.setOnCheckedChangeListener(listener);
    }


    class RadioBtnListener implements CompoundButton.OnCheckedChangeListener{


        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(buttonView.getId() == radioBtn01.getId()){
                if(isChecked){
                    textView02.setText(buttonView.getText().toString() + " is checked.");
                }
                else{
                    textView02.setText(buttonView.getText().toString() + " is unchecked.");
                }
            }
            else{
                if(isChecked){
                    textView04.setText(buttonView.getText().toString() + " is checked.");
                }
                else{
                    textView04.setText(buttonView.getText().toString() + " is unchecked.");
                }
            }
        }
    }


    class RadioGroupListener implements RadioGroup.OnCheckedChangeListener{


        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(checkedId == radioBtn01.getId()){
                textView01.setText(radioBtn01.getText().toString());
            }
            else if(checkedId == radioBtn02.getId()){
                textView01.setText(radioBtn02.getText().toString());
            }
            else if(checkedId == radioBtn03.getId()){
                textView03.setText(radioBtn03.getText().toString());
            }
            else if(checkedId == radioBtn04.getId()){
                textView03.setText(radioBtn04.getText().toString());
            }
        }
    }
}


res/layout/main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Gentle: "
            android:textSize="20sp"/>
    <RadioGroup
            android:id="@+id/radioGroup01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <RadioButton
                android:id="@+id/radioBtn01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female"/>
        <RadioButton
                android:id="@+id/radioBtn02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Male"
                android:layout_marginLeft="90dp"/>
            </RadioGroup>


    <TextView
            android:id="@+id/textView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"/>
    <TextView
            android:id="@+id/textView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"/>
    <RadioGroup
            android:id="@+id/radioGroup02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <RadioButton
                android:id="@+id/radioBtn03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="To Be"
                android:textSize="20sp"/>
        <RadioButton
                android:id="@+id/radioBtn04"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Not To Be"
                android:textSize="20sp"
                android:layout_marginLeft="90dp"/>
    </RadioGroup>
    <TextView
            android:id="@+id/textView03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"/>
    <TextView
            android:id="@+id/textView04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"/>
</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值