安卓入门.多选按钮CheckBox
多选按钮CheckBox 1.在XML文件中,使用<CheckBox/>标签 <CheckBox android:id="@+id/eat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="吃饭" /> <CheckBox android:id="@+id/sleep" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="睡觉" /> <CheckBox android:id="@+id/dota" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="dota" /> 2.在代码中,使用CheckBox的对象来表示 private CheckBox eatBox; private CheckBox sleepBox; private CheckBox dotaBox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); eatBox=(CheckBox)findViewById(R.id.eat); sleepBox=(CheckBox)findViewById(R.id.sleep); dotaBox=(CheckBox)findViewById(R.id.dota); } 3.常用监听器OnClickListener接口和OnCheckedChangeListener 接口 用类来实现这些接口 //OnClickListener class CheckBoxClickListener implements OnClickListener { @Override public void onClick(View v) {//view是CheckBox的父类,所以CheckBox可以向上转型 if (v.getId() == R.id.eat) { System.out.println("eat"); } else if (v.getId() == R.id.sleep) { System.out.println("sleep"); } else if (v.getId() == R.id.dota) { System.out.println("dota"); } } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); eatBox = (CheckBox) findViewById(R.id.eat); sleepBox = (CheckBox) findViewById(R.id.sleep); dotaBox = (CheckBox) findViewById(R.id.dota); CheckBoxClickListener checkBoxClickListener = new CheckBoxClickListener(); eatBox.setOnClickListener(checkBoxClickListener); sleepBox.setOnClickListener(checkBoxClickListener);//监听器绑定控件 dotaBox.setOnClickListener(checkBoxClickListener); } ================ //实现OnCheckedChangeListener接口 // 单选框,isChecked(被选中)或者UnCheck class CheckBoxChange implements OnCheckedChangeListener { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (buttonView.getId() == R.id.dota) { System.out.println("dotaBox"); } else if (buttonView.getId() == R.id.eat) { System.out.println("eatBox"); } else if (buttonView.getId() == R.id.sleep) { System.out.println("sleepBox"); } if (isChecked) { System.out.println("checked"); } else { System.out.println("Unchecked"); } } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); eatBox = (CheckBox) findViewById(R.id.eat); sleepBox = (CheckBox) findViewById(R.id.sleep); dotaBox = (CheckBox) findViewById(R.id.dota); // CheckBoxClickListener checkBoxClickListener = new // CheckBoxClickListener(); // eatBox.setOnClickListener(checkBoxClickListener);//监听器绑定控件 // sleepBox.setOnClickListener(checkBoxClickListener);//监听器绑定控件 // dotaBox.setOnClickListener(checkBoxClickListener);//监听器绑定控件 CheckBoxChange checkBoxChange = new CheckBoxChange(); eatBox.setOnCheckedChangeListener(checkBoxChange);//监听器绑定控件 sleepBox.setOnCheckedChangeListener(checkBoxChange);//监听器绑定控件 dotaBox.setOnCheckedChangeListener(checkBoxChange);//监听器绑定控件 }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28