CheckBox应用详解

CheckBox的详细用法,以下用案例来说明:

先看一张效果图

CheckBox应用详解_第1张图片CheckBox应用详解_第2张图片

XML代码:

<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"
    android:orientation="vertical"
    tools:context="com.example.checkbox.MainActivity" >
    
           <CheckBox 
               android:id="@+id/cb1"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:text="java"
               />
           <CheckBox 
               android:id="@+id/cb2"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:text="android"
               />
           <CheckBox 
               android:id="@+id/cb3"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:checked="true"
               android:text="html5"
               />
           <CheckBox 
               android:id="@+id/cb4"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:text="php"
               />
           <CheckBox 
               android:id="@+id/cb5"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:text="全选"
               />
           <CheckBox 
               android:id="@+id/cb6"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:text="反选"
               />
           
        <Button
            android:id="@+id/bt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="提交"
            />
LinearLayout>

android代码:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {

    private CheckBox cb1,cb2,cb3,cb4,cb5,cb6;
    private Button bt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt = (Button) findViewById(R.id.bt);
        cb1 = (CheckBox) findViewById(R.id.cb1);
        cb2 = (CheckBox) findViewById(R.id.cb2);
        cb3 = (CheckBox) findViewById(R.id.cb3);
        cb4 = (CheckBox) findViewById(R.id.cb4);
        cb5 = (CheckBox) findViewById(R.id.cb5);
        cb6 = (CheckBox) findViewById(R.id.cb6);
        
        bt.setOnClickListener(new OnClickListener() {
            //此处点击事件的监听类为:android.view.View.OnClickListener
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), (cb1.isChecked()?cb1.getText().toString()+" ":"")+(cb2.isChecked()?cb2.getText().toString()+" ":"")+(cb3.isChecked()?cb3.getText().toString()+" ":"")+(cb4.isChecked()?cb4.getText().toString()+" ":""), 0).show();
            }
        });
        
        cb5.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            //此处点击事件的监听类为:CompoundButton.OnCheckedChangeListener
            //isChecked:判断按钮是否为选中状态
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked){
                    cb1.setChecked(true);
                    cb2.setChecked(true);
                    cb3.setChecked(true);
                    cb4.setChecked(true);
                }else{
                    cb1.setChecked(false);
                    cb2.setChecked(false);
                    cb3.setChecked(false);
                    cb4.setChecked(false);
                }
                    
            }
        });
        
        cb6.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                cb1.setChecked(!cb1.isChecked());
                cb2.setChecked(!cb2.isChecked());
                cb3.setChecked(!cb3.isChecked());
                cb4.setChecked(!cb4.isChecked());
            }
        });
    }
}

转载于:https://www.cnblogs.com/wmkill/articles/4971450.html

你可能感兴趣的:(CheckBox应用详解)