CheckBox 多选按钮的使用方法

创建布局文件*声明一组多选按钮,设置各个按钮id .*

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >
***创建一组多选按钮,设置各个按钮id   ,统一由allcheck(全选按钮控制)***
    <CheckBox
        android:id="@+id/eat"
        android:text="吃饭"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="5dp"
      />
    <CheckBox
        android:id="@+id/sleep"
        android:text="睡觉"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        />
    <CheckBox
        android:id="@+id/dota"
        android:text="打dota"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        />**重点内容**
    <CheckBox
        android:id="@+id/allChenked"
        android:text="全选/全不选"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        />
</LinearLayout>

MainActivity 中使用findViewById 获取对象,再为CheckBox 绑定相应的监听器(OnCheckedListener 以及OnCheckedChangeListener(在按钮选中状态发生改变时被调用))

public class MainActivity extends ActionBarActivity {

    private CheckBox eatBox;
    private CheckBox sleepBox;
    private CheckBox dotaBox;
    private CheckBox allCheckedBox;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



**//此处通过findViewById 找到相应的CheckBox**
------------------------------------


        eatBox=(CheckBox)findViewById(R.id.eat);
        sleepBox=(CheckBox)findViewById(R.id.sleep);
        dotaBox=(CheckBox)findViewById(R.id.dota);
        allCheckedBox=(CheckBox)findViewById(R.id.allChenked);

//生成监听器对象,给AllCheckBoxListener 实现监听器接口
        AllCheckBoxListener listener = new AllCheckBoxListener();
        allCheckedBox.setOnCheckedChangeListener(listener);



//定义AllCheckBoxListener 类,用来实现监听器接口。 

    class  AllCheckBoxListener implements CompoundButton.OnCheckedChangeListener{

// CompoundButton.OnCheckedChangeListener 用来监听CheckBox 对象的状态变化 

        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//该方法内实现当allCheckedBox 状态变化时,eatBox,sleepBox,dataBox    三个CheckBox状态随之变化

            eatBox.setChecked(b);
            sleepBox.setChecked(b);
            dotaBox.setChecked(b);
//或者更改为复杂的代码方式
//          if (allCheckedBox=isChecked()){
//      eatbox.setChecked(ture);
//      sleepbox.setChecked(ture);
//      dotabox.setChecked(ture);
//      }else{
//          eatbox.setChecked(false);
//          sleepbox.setChecked(false);
//          dotabox.setChecked(false);
//      }
//
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想要实现多选按钮,可以使用 `CheckBox` 控件,当用户点击 `Button` 按钮时,检查哪些 `CheckBox` 被选中,并将选中的文字显示在 `TextView` 控件中,最后使用 `Toast` 提示用户选择成功。 以下是一个简单的实现示例: ```java public class MainActivity extends AppCompatActivity { private CheckBox checkBox1, checkBox2, checkBox3; private Button confirmBtn; private TextView resultText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); checkBox1 = findViewById(R.id.checkbox1); checkBox2 = findViewById(R.id.checkbox2); checkBox3 = findViewById(R.id.checkbox3); confirmBtn = findViewById(R.id.btn_confirm); resultText = findViewById(R.id.text_result); confirmBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { StringBuilder sb = new StringBuilder("选中了:"); if (checkBox1.isChecked()) { sb.append("选项1 "); } if (checkBox2.isChecked()) { sb.append("选项2 "); } if (checkBox3.isChecked()) { sb.append("选项3 "); } if (sb.toString().equals("选中了:")) { sb.append("无"); } resultText.setText(sb.toString()); Toast.makeText(MainActivity.this, "选择成功", Toast.LENGTH_SHORT).show(); } }); } } ``` 在上述代码中,我们首先获取了三个 `CheckBox` 控件、一个 `Button` 按钮和一个 `TextView` 控件。然后,在 `confirmBtn` 的监听器中,我们检查哪些 `CheckBox` 被选中,将选中的文字显示在 `resultText` 中,并使用 `Toast` 提示用户选择成功。需要注意的是,如果用户没有选择任何选项,我们需要在结果中显示“无”。 最后,这个多选按钮的实现就完成了。您可以根据自己的需求对代码进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值