Android中CheckBox复选框操作
16lz
2022-04-04
public class MainActivity extends Activity {// 声明多选列表对象private CheckBox cbx1, cbx2, cbx3, cbx4;private List checkBoxs = new ArrayList();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);cbx1 = (CheckBox) findViewById(R.id.checkBox1);cbx2 = (CheckBox) findViewById(R.id.checkBox2);cbx3 = (CheckBox) findViewById(R.id.checkBox3);cbx4 = (CheckBox) findViewById(R.id.checkBox4);// 默认选项cbx1.setChecked(true);cbx3.setChecked(true);cbx1.setOnCheckedChangeListener(listener);cbx2.setOnCheckedChangeListener(listener);cbx3.setOnCheckedChangeListener(listener);cbx4.setOnCheckedChangeListener(listener);// 添加到集合中checkBoxs.add(cbx1);checkBoxs.add(cbx2);checkBoxs.add(cbx3);checkBoxs.add(cbx4);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}public void getValues(View v) {String content = "";for (CheckBox cbx : checkBoxs) {if (cbx.isChecked()) {content += cbx.getText() + "\n";}}if ("".equals(content)) {content = "您还没有选择呢";}new AlertDialog.Builder(this).setMessage(content).setTitle("选中的内容如下").setPositiveButton("关闭", null).show();}CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {CheckBox box = (CheckBox) buttonView;Toast.makeText(getApplicationContext(),"获取的值:" + isChecked + "xxxxx" + box.getText(),Toast.LENGTH_LONG).show();}};}
更多相关文章
- android三个选项的对话框
- java.util.ConcurrentModificationException 解决 Android
- Android——CheckBox【复选框】 点击事件与属性,用案例说明
- Android(安卓)自定义日期控件,包含月控件
- android中gridView中点击 item为选中状态
- android checkBox 的选中和取消选中
- shoppingcart
- 改变android 选项卡的背景色
- Android(安卓)监听各个Acitivity的生命周期