CheckBox设计及事件响应

整体布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="同意"
        android:id="@+id/tongyi"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:id="@+id/Linear"
        android:layout_height="wrap_content">
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="AA"
            android:id="@+id/aa"
            />
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BB"
            android:id="@+id/bb"
            />
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CC"
            android:id="@+id/cc"
            />
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="DD"
            android:id="@+id/dd"
            />
    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_sub"
        android:text="提交"
        />
</LinearLayout>

事件响应处理主要采用匿名内部类的方式

1.主动汇报

 //主动汇报
        final StringBuffer sbuffer=new StringBuffer();
        //拿到所有的子类长度
        int cNum = Linear.getChildCount();
        for (int i = 0; i < cNum; i++) {
            //根据i 拿到每一个CheckBox
            final CheckBox cbox= (CheckBox) Linear.getChildAt(i);
            cbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    //判断CheckBox是否被选中
                    if(b){
                        if(n==1) {
                            sbuffer.append("选中");
                            n=n+1;
                        }
                        //把被选中的文字添加到StringBuffer中
                        sbuffer.append(cbox.getText().toString());
                    }
                    else {
                        sbuffer.delete(sbuffer.indexOf(cbox.getText().toString()),sbuffer.indexOf(cbox.getText().toString())+2);
                    }
                    if(!sbuffer.toString().contains("AA")&&!sbuffer.toString().contains("BB")&&!sbuffer.toString().contains("CC")&&!sbuffer.toString().contains("DD")){
                        sbuffer.delete(sbuffer.indexOf("选中"),sbuffer.indexOf("选中")+2);
                        n=1;
                    }
                    Toast.makeText(MainActivity.this, sbuffer.toString(), Toast.LENGTH_SHORT).show();
                }
            });

        }
  		//主动汇报简单的例子
        ckOK.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    ckOK.setText("已同意");
                }else{
                    ckOK.setText("同意");
                }
            }
        });

效果

在这里插入图片描述

2.被动告知

 //被动那个获知
        btn_sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer sb=new StringBuffer();
                //拿到所有的子类长度
                int cNum = Linear.getChildCount();
                for (int i = 0; i < cNum; i++) {
                    //根据i 拿到每一个CheckBox
                    CheckBox cb= (CheckBox) Linear.getChildAt(i);
                    //判断CheckBox是否被选中
                    if(cb.isChecked()){
                        //把被选中的文字添加到StringBuffer中
                        sb.append(cb.getText().toString());
                    }
                }
                Toast.makeText(getApplicationContext(), sb.toString(), Toast.LENGTH_SHORT).show();
            }
        });

效果

在这里插入图片描述

3.整体代码

public class MainActivity extends AppCompatActivity {
    int n=1;
    private CheckBox ckOK;
    private LinearLayout Linear;
    private Button btn_sub;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkbox);

        ckOK= (CheckBox) findViewById(R.id.tongyi);
        Linear= (LinearLayout) findViewById(R.id.Linear);
        btn_sub= (Button) findViewById(R.id.btn_sub);

        //主动汇报
        final StringBuffer sbuffer=new StringBuffer();
        //拿到所有的子类长度
        int cNum = Linear.getChildCount();
        for (int i = 0; i < cNum; i++) {
            //根据i 拿到每一个CheckBox
            final CheckBox cbox= (CheckBox) Linear.getChildAt(i);
            cbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    //判断CheckBox是否被选中
                    if(b){
                        if(n==1) {
                            sbuffer.append("选中");
                            n=n+1;
                        }
                        //把被选中的文字添加到StringBuffer中
                        sbuffer.append(cbox.getText().toString());
                    }
                    else {
                        sbuffer.delete(sbuffer.indexOf(cbox.getText().toString()),sbuffer.indexOf(cbox.getText().toString())+2);
                    }
                    if(!sbuffer.toString().contains("AA")&&!sbuffer.toString().contains("BB")&&!sbuffer.toString().contains("CC")&&!sbuffer.toString().contains("DD")){
                        sbuffer.delete(sbuffer.indexOf("选中"),sbuffer.indexOf("选中")+2);
                        n=1;
                    }
                    Toast.makeText(MainActivity.this, sbuffer.toString(), Toast.LENGTH_SHORT).show();
                }
            });
        }
		//主动汇报简单的例子
        ckOK.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    ckOK.setText("已同意");
                }else{
                    ckOK.setText("同意");
                }
            }
        });

        //被动获知
        btn_sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer sb=new StringBuffer();
                //拿到所有的子类长度
                int cNum = Linear.getChildCount();
                for (int i = 0; i < cNum; i++) {
                    //根据i 拿到每一个CheckBox
                    CheckBox cb= (CheckBox) Linear.getChildAt(i);
                    //判断CheckBox是否被选中
                    if(cb.isChecked()){
                        //把被选中的文字添加到StringBuffer中
                        sb.append(cb.getText().toString());
                    }
                }
                Toast.makeText(getApplicationContext(), sb.toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
发布了16 篇原创文章 · 获赞 5 · 访问量 725

猜你喜欢

转载自blog.csdn.net/qq_18625571/article/details/104466866