zoukankan      html  css  js  c++  java
  • 3.26

    CheckBox(复选框):

    复选框,即可以同时选中多个选项,至于获得选中的值,同样有两种方式: 1.为每个CheckBox添加事件:setOnCheckedChangeListener 2.弄一个按钮,在点击后,对每个checkbox进行判断:isChecked();

    代码示例:

    public class MainActivity extends AppCompatActivity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener{
    
        private CheckBox cb_one;
        private CheckBox cb_two;
        private CheckBox cb_three;
        private Button btn_send;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            cb_one = (CheckBox) findViewById(R.id.cb_one);
            cb_two = (CheckBox) findViewById(R.id.cb_two);
            cb_three = (CheckBox) findViewById(R.id.cb_three);
            btn_send = (Button) findViewById(R.id.btn_send);
    
            cb_one.setOnCheckedChangeListener(this);
            cb_two.setOnCheckedChangeListener(this);
            cb_three.setOnCheckedChangeListener(this);
            btn_send.setOnClickListener(this);
    
        }
    
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
           if(compoundButton.isChecked()) Toast.makeText(this,compoundButton.getText().toString(),Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onClick(View view) {
            String choose = "";
            if(cb_one.isChecked())choose += cb_one.getText().toString() + "";
            if(cb_two.isChecked())choose += cb_two.getText().toString() + "";
            if(cb_three.isChecked())choose += cb_three.getText().toString() + "";
            Toast.makeText(this,choose,Toast.LENGTH_SHORT).show();
        }
    }

     

  • 相关阅读:
    Git问题(Your local changes to the followingcant checkout because of unmerged files)
    Java实体映射工具 MapStruct
    获取一段代码执行花费的时长
    mybatis的xml中trim标签
    SQL语句统计每天、每月、每年的数据
    Java常见异常
    跨域与跨域访问
    java8新特性
    java中i=i++问题分析
    钉钉——第三方应用嵌入钉钉【微应用】
  • 原文地址:https://www.cnblogs.com/20193898liufa/p/14908936.html
Copyright © 2011-2022 走看看