我用自己的方法实现了Android复选框

###### 我用自己的方法实现了Android复选框,简单又实用。
XML布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="match_parent"

        android:layout_height="100dp">

        <CheckBox
            android:id="@+id/nc"
            android:layout_width="120dp"
            android:layout_height="50dp"
            android:text="南昌"/>
        <CheckBox
            android:id="@+id/bj"
            android:layout_width="120dp"
            android:layout_height="50dp"
            android:text="北京"/>
        <CheckBox
            android:id="@+id/sh"
            android:layout_width="120dp"
            android:layout_height="50dp"
            android:text="上海"/>
    </LinearLayout>

    <Button
        android:onClick="qr"
        android:layout_width="120dp"
        android:layout_height="50dp"
        android:text="确认"/>
</LinearLayout>
接下来我们看看java代码如何实现
package com.example.newland.checkbox;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    CheckBox nc;
    CheckBox bj;
    CheckBox sh;

    ArrayList<String> st = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nc = findViewById(R.id.nc);
        bj = findViewById(R.id.bj);
        sh = findViewById(R.id.sh);

      nc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
              if(b){
                st.add("南昌");
              }else {
                  st.remove("南昌");
              }
          }
      });
      bj.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
              if(b){
                  st.add("北京");
              }else {
                  st.remove("北京");
              }
          }
      });
      sh.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
              if(b){
                  st.add("上海");
              }else {
                  st.remove("上海");
              }
          }
      });
    }

    public void qr(View view) {
        String s  ="";
        for (String sf:st){
            s +=sf;
        }
        Toast.makeText(this, "选中了"+s, Toast.LENGTH_SHORT).show();
    }
}
是不是很容易理解,对集合进行一个添加删除操作既可,有什么不同建议,可以分享给我;

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×