android之复选框点击事件(掌握CheckBox复选控件)

基本属性以及方法:

<1>CheckBox

        CheckBox,复选按钮。扩展自CompoundButton类。具有选中和非选中两种状态。复选按钮在单个使用时与状态按钮功能相近。与状态按钮不同在于复选按钮可以多个同时使用。当使用多个复选按钮时,可实现多选功能。其主要xml属性是继承自CompoundButton的android:checked属性,该属性用于设置复选按钮的状态。其主要方法是isChecked(),该方法用于获取复选按钮的当前状态。 

·复选按钮

  • 继承自CompoundButton
  • 两个主要用途: 
    做状态按钮使用 
    做复选框使用 
    ·主要属性:
  • Android:checked 
    ·主要方法:
  • isChecked() 
    ·主要事件
  • setOnCheckedChangeListener 
    (OnCheckedChangeListener l)

<2>RadioButton和RadioGroup 

        RadioButton,单选按钮。扩展自CompoundButton类。单选按钮控件通常与RadioGroup搭配使用。RadioGroup是LinearLayout的子类,用于将多个单选按钮组合为一组。同一按钮组内的单选按钮只能有一个被选中。RadioGroup最主要的xml属性是android:checkedButton,该属性用于设置组内默认被选中的单选按钮的id。RadioGroup最主要的方法有getCheckedRadioButtonId()和check(int radioButtonId)。

RaidoButton 
·单选按钮

  • 继承自CompoudButton
  • 需要与RadioGroup搭配使用 
    ·主要属性:
  • android:checked 
    ·主要方法:
  • isChecked()

RaidoGroup 
·单选按钮组

  • 继承自Linearyout
  • 需要设置组内单选按钮的排列方向 
    ·主要属性:
  • android:checkedButton 
    ·主要方法:
  • getCheckedRadioButtonId()
  • check(int rdoId)

android之复选框点击事件(掌握CheckBox复选控件)_第1张图片

示例代码:

前端代码:





    
    

    

    

    

    

后台逻辑:

package com.example.lenovo.checkedbox;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private CheckBox checkBox_singing,checkBox_code,checkBox_climb,checkBox_ball;

    private List loves=new ArrayList<>();

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

        this.initView();

        this.initValue();

    }

    /**
     * 用户点击确定按钮时执行的函数
     * @param view
     */
    public void ok(View view){

        this.initValue();

        /*
        Button button=(Button) view;
        String content=button.getText().toString();

        Toast.makeText(this,content,Toast.LENGTH_LONG).show();
        */
    }

    /**
     * 得到默认情况下选中的爱好
     */
    private void initValue() {

        if(this.checkBox_singing.isChecked()){
            String text=this.checkBox_singing.getText().toString();
            loves.add(text);
        }

        if(this.checkBox_code.isChecked()){
            String text=this.checkBox_code.getText().toString();
            loves.add(text);
        }

        if(this.checkBox_climb.isChecked()){
            String text=this.checkBox_climb.getText().toString();
            loves.add(text);
        }

        if(this.checkBox_ball.isChecked()){
            String text=this.checkBox_ball.getText().toString();
            loves.add(text);
        }


        Toast.makeText(this,loves.toString(),Toast.LENGTH_LONG).show();

        //清除之前的内容
        loves.clear();

    }

    /**
     * 初始化控件
     */
    private void initView() {
        this.checkBox_singing= (CheckBox) this.findViewById(R.id.checkBox_singing);
        //注册复选框状态发生变化时对应的监听器对象,让当前Activity 实现接口CompoundButton.OnCheckedChangeListener
        this.checkBox_singing.setOnCheckedChangeListener(this);



        this.checkBox_code= (CheckBox) this.findViewById(R.id.checkBox_code);
        this.checkBox_code.setOnCheckedChangeListener(this);


        this.checkBox_climb= (CheckBox) this.findViewById(R.id.checkBox_climb);
        this.checkBox_climb.setOnCheckedChangeListener(this);

        this.checkBox_ball= (CheckBox) this.findViewById(R.id.checkBox_ball);
        this.checkBox_ball.setOnCheckedChangeListener(this);
    }

    /**
     * 当复选框的状态发生变化时自动调用的方法
     * @param buttonView 事件源
     * @param isChecked 当前复选框是否选中, true:选中,false 不选中
     */
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
            //用户点击的复选框的内容
            String text=buttonView.getText().toString();
            System.out.println("text="+text+",isChecked="+isChecked);
            System.out.println("当前控件处于选中状态");
        }else{
            System.out.println("当前控件取消了选中状态");
        }

    }
}


你可能感兴趣的:(android)