CheckBox练习(android)

CheckBox练习

效果图:

checkbox 和 radioButton 相比,不需要 RadioGroup ,每个 checkbox 是单独的控件,使用很简单 isChecked 是获取是否选择中,选择时有 CheckedChangeListener 监听,具体请看代码

java 代码,该代码简化了在每个chekcbox 中写 setOnCheckedChangeListener 监听代码

package zziss.android.checkboxtest;

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

public class CheckBoxTestActivity extends Activity {
    /** Called when the activity is first created. */
    private CheckBox cb_red;
    private CheckBox cb_blue;
    private CheckBox cb_gray;
    private CheckBox cb_green;
    
    private Button   btn_chose;
    private CheckListener listener; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        cb_red = (CheckBox)this.findViewById(R.id.cb_red);
        cb_blue = (CheckBox)this.findViewById(R.id.cb_blue);
        cb_gray = (CheckBox)this.findViewById(R.id.cb_gray);
        cb_green = (CheckBox)this.findViewById(R.id.cb_green);
        btn_chose = (Button)this.findViewById(R.id.btn);
        
        listener = new CheckListener();
        listener.iActivity = this;
        cb_red.setOnCheckedChangeListener(listener);
        cb_blue.setOnCheckedChangeListener(listener);
        cb_gray.setOnCheckedChangeListener(listener);
        cb_green.setOnCheckedChangeListener(listener);
        
        btn_chose.setOnClickListener(listener);
    }
    
    public void showInfo(String str)
    {
        Toast toast = Toast.makeText(this,str,Toast.LENGTH_SHORT);
        toast.show();
    }
    
    public void showCheckTexts()
    {
        String s = "";
        s += cb_red.isChecked()?cb_red.getText().toString()+";":"";
        s += cb_blue.isChecked()?cb_blue.getText().toString()+";":"";
        s += cb_gray.isChecked()?cb_gray.getText().toString()+";":"";
        s += cb_green.isChecked()?cb_green.getText().toString()+";":"";
        showInfo(s);
    }
}

class CheckListener  implements
android.widget.CompoundButton.OnCheckedChangeListener,
android.view.View.OnClickListener

{

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
        iActivity.showInfo(buttonView.getText().toString());
    }
    public CheckBoxTestActivity iActivity;
    
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        iActivity.showCheckTexts();
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width
="fill_parent"
    android:layout_height
="fill_parent"
    android:orientation
="vertical" >

    <TextView
        
android:layout_width="fill_parent"
        android:layout_height
="wrap_content"
        android:text
="@string/info" />
    <CheckBox 
        
android:id="@+id/cb_red"
        android:layout_width
="fill_parent"
        android:layout_height
="wrap_content"
        android:text
="@string/cb_red"
        
/>
    <CheckBox 
        
android:id="@+id/cb_blue"
        android:layout_width
="fill_parent"
        android:layout_height
="wrap_content"
        android:text
="@string/cb_blue"
        
/>
    <CheckBox 
        
android:id="@+id/cb_gray"
        android:layout_width
="fill_parent"
        android:layout_height
="wrap_content"
        android:text
="@string/cb_gray"
        
/>
    <CheckBox 
        
android:id="@+id/cb_green"
        android:layout_width
="fill_parent"
        android:layout_height
="wrap_content"
        android:text
="@string/cb_green"
        
/>
    <Button
        
android:id="@+id/btn"
        android:layout_width
="wrap_content"
        android:layout_height
="wrap_content"
        android:text
="@string/btn_chose"
        
/>
</LinearLayout>

String.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="info">checkbox练习</string>
    <string name="app_name">CheckBoxTest</string>
    <string name="cb_red">红色</string>
    <string name="cb_blue">蓝色</string>
    <string name="cb_gray">灰色</string>
    <string name="cb_green">绿色</string>
    <string name="btn_chose">查看选择</string>
</resources>
原文地址:https://www.cnblogs.com/zziss/p/2289050.html