加监听器,单选复选按钮

MainActivity

package com.example.lenovo.myapplication;

import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;

public class TextActivity1 extends AppCompatActivity {

    RadioGroup rg_1;
    RadioButton nan;
    RadioButton nv;
    CheckBox cb_1;
    CheckBox cb_2;
    ToggleButton tb_1;
    Switch sw_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text1);

        rg_1=(RadioGroup)findViewById(R.id.rg_1);
        nan=(RadioButton)findViewById(R.id.nan);
        nv=(RadioButton)findViewById(R.id.nv);
        cb_1=(CheckBox)findViewById(R.id.cb_1);
        cb_2=(CheckBox)findViewById(R.id.cb_2);
        tb_1=(ToggleButton)findViewById(R.id.tb_1);
        sw_1=(Switch)findViewById(R.id.sw_1);

        tb_1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                Toast.makeText(TextActivity1.this, "ToggleButton开关状态:"+(isChecked?"开":"关"), Toast.LENGTH_SHORT).show();
            }
        });

        sw_1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(TextActivity1.this, "Switch开关状态:"+(isChecked?"开":"关"), Toast.LENGTH_SHORT).show();
            }
        });

        //加监听器.监听器的实例
        CB_OnCheckedChangeListener cb1=new CB_OnCheckedChangeListener();
        //监听器绑定
        cb_1.setOnCheckedChangeListener(cb1);
        cb_2.setOnCheckedChangeListener(cb1);

        rg_1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            //被选中的组件id。RadioButton的id
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //提示选中的内容
                //判断谁被选中
                if (checkedId==nan.getId())
                {
                    Toast.makeText(TextActivity1.this, "选中的是:"+nan.getText(), Toast.LENGTH_SHORT).show();
                }
                else if (checkedId==nv.getId())
                {
                    Toast.makeText(TextActivity1.this, "选中的是:"+nv.getText(), Toast.LENGTH_SHORT).show();

                }

            }
        });
    }
    //公共的复选按钮的监听器
    class CB_OnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            CheckBox cb=(CheckBox)buttonView;
            String str=cb.getText().toString();
            if (isChecked)
            {
                Toast.makeText(TextActivity1.this, "被选中", Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(TextActivity1.this, "被取消选中", Toast.LENGTH_SHORT).show();

            }
        }
    }
}
View Code

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.lenovo.myapplication.TextActivity1"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/nnn"/>
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/abc"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/nnn"
        android:text="普通按钮可以加字"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/qq"
        android:alpha="1"
        android:background="#F00"
        android:scaleType="center"/>
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/qq"
        android:alpha="1"
        android:background="#F00"
        android:scaleType="centerCrop"/>
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/abc"
            android:alpha="1"
            android:background="#F00"
            android:scaleType="centerInside"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/abc"
            android:alpha="1"
            android:background="#F00"
            android:scaleType="matrix"/>
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/abc"
            android:alpha="1"
            android:background="#F00"
            android:scaleType="fitCenter"/>
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/abc"
            android:alpha="1"
            android:background="#F00"
            android:scaleType="fitXY"/>
    </LinearLayout>
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rg_1"
        android:orientation="horizontal">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="单选,男"
            android:checked="true"
            android:id="@+id/nan"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="单选,女"
            android:id="@+id/nv"/>
    </RadioGroup>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="范冰冰"
        android:id="@+id/cb_1"
        android:checked="true"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="章子怡"
        android:id="@+id/cb_2"/>


        </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="开"
        android:textOff="关"
        android:id="@+id/tb_1"/>
    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="关"
        android:textOn="开"
        android:text="开关"
        android:id="@+id/sw_1"/>

    </LinearLayout>

</LinearLayout>
View Code
原文地址:https://www.cnblogs.com/1ming/p/5480944.html