Android控件之Checkbox和RadioButton

来源:互联网 发布:java json传值到前台 编辑:程序博客网 时间:2024/05/29 07:58

Checkbox和RadioButton监听事件都是:

OnCheckedChangeListener//当选中的改变监听

参考代码:

package com.example.checkboxandradiobutton;import android.app.Activity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.RadioButton;import android.widget.Toast;public class MainActivity extends Activity implements OnCheckedChangeListener {    private CheckBox music,game,vidio;    private RadioButton famale,male;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initUI();    }    //初始化UI控件    public void initUI(){        //CheckBox控件        music=(CheckBox) findViewById(R.id.music);        game=(CheckBox) findViewById(R.id.game);        vidio=(CheckBox) findViewById(R.id.vidio);        music.setOnCheckedChangeListener(this);        game.setOnCheckedChangeListener(this);        vidio.setOnCheckedChangeListener(this);        //RadioButton控件        famale=(RadioButton) findViewById(R.id.famale);        male=(RadioButton) findViewById(R.id.male);        famale.setOnCheckedChangeListener(this);        male.setOnCheckedChangeListener(this);    }    @Override    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {        // TODO Auto-generated method stub        switch (buttonView.getId()) {        case R.id.music:            Toast.makeText(getApplicationContext(), music.getText()+"/"+isChecked, 1000).show();            break;        case R.id.game:            Toast.makeText(getApplicationContext(), game.getText()+"/"+isChecked, 1000).show();            break;        case R.id.vidio:            Toast.makeText(getApplicationContext(), vidio.getText()+"/"+isChecked, 1000).show();            break;        case R.id.famale:            Toast.makeText(getApplicationContext(), famale.getText()+"/"+isChecked, 1000).show();            break;        case R.id.male:            Toast.makeText(getApplicationContext(), male.getText()+"/"+isChecked, 1000).show();            break;        }    }}