1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| package com.sunjiajia.monkeyandroid;
import android.os.Bundle; import android.support.annotation.Nullable; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast;
public class CheckBoxRadioButtonActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener, RadioGroup.OnCheckedChangeListener { @Override public int giveViewResId() { return R.layout.activity_checkbox_radiobutton; }
private CheckBox mCbXs, mCbYx, mCbDy; private RadioGroup mRg; private RadioButton mRbBanana, mRbApple, mRbOrange;
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);
initViews();
configViews(); }
private void configViews() { mCbXs.setOnCheckedChangeListener(this); mCbYx.setOnCheckedChangeListener(this); mCbDy.setOnCheckedChangeListener(this);
mRg.setOnCheckedChangeListener(this); }
private void initViews() { mCbXs = (CheckBox) findViewById(R.id.checkbox_xiaoshuo); mCbYx = (CheckBox) findViewById(R.id.checkbox_youxi); mCbDy = (CheckBox) findViewById(R.id.checkbox_dianying);
mRg = (RadioGroup) findViewById(R.id.radio_group);
mRbBanana = (RadioButton) findViewById(R.id.radiobutton_banana); mRbApple = (RadioButton) findViewById(R.id.radiobutton_apple); mRbOrange = (RadioButton) findViewById(R.id.radiobutton_orange); }
@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) { case R.id.checkbox_xiaoshuo: checkedShowToast(buttonView, isChecked); break; case R.id.checkbox_youxi: checkedShowToast(buttonView, isChecked); break; case R.id.checkbox_dianying: checkedShowToast(buttonView, isChecked); break; } }
@Override public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) { case R.id.radiobutton_banana: radioGroupChecked(mRbBanana); break; case R.id.radiobutton_apple: radioGroupChecked(mRbApple); break; case R.id.radiobutton_orange: radioGroupChecked(mRbOrange); break; } }
private void radioGroupChecked(RadioButton button) { Toast.makeText(CheckBoxRadioButtonActivity.this, "您选中了#" + button.getText().toString() + "#", Toast.LENGTH_SHORT).show(); }
private void checkedShowToast(CompoundButton buttonView, boolean isChecked) { if (isChecked) { Toast.makeText(CheckBoxRadioButtonActivity.this, "您选中了#" + buttonView.getText().toString() + "#", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(CheckBoxRadioButtonActivity.this, "您取消了#" + buttonView.getText().toString() + "#", Toast.LENGTH_SHORT).show(); } } }
|