CheckBox_全选_仿购物车

MainActivity

/**
 * @功能 CheckBox 复选框
 * @说明 仿购物车结算
 */
public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {
    private CheckBox checkBox1, checkBox2, checkBox3, checkBox4;
    private Button button;
    private TextView textView;
    private static int sum = 0;

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

    private void initView() {
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
        checkBox1 = (CheckBox) findViewById(R.id.checkbox1);
        checkBox2 = (CheckBox) findViewById(R.id.checkbox2);
        checkBox3 = (CheckBox) findViewById(R.id.checkbox3);
        checkBox4 = (CheckBox) findViewById(R.id.checkbox4);
        checkBox1.setOnCheckedChangeListener(this);
        checkBox2.setOnCheckedChangeListener(this);
        checkBox3.setOnCheckedChangeListener(this);
        checkBox4.setOnClickListener(this);
        textView = (TextView) findViewById(R.id.tv);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        //如果发现列表中的CheckBox全部选中,则全选按钮就被选中
        if (checkBox1.isChecked() && checkBox2.isChecked() && checkBox3.isChecked()) {
            checkBox4.setChecked(true);
        } else {
            checkBox4.setChecked(false);
        }
        //勾选就把价钱相加
        if (isChecked) {
            //获得当前勾选的CheckBox
            CheckBox box = (CheckBox) buttonView;
            //获得CheckBox的内容,转换为int类型
            int str = Integer.parseInt(box.getText().toString());
            //每次勾选一个都会把价钱加起来
            sum += str;
            //转换为字符串类型
            String sum1 = Integer.toString(sum);
            //设置显示价格
            textView.setText(sum1);
            //取消勾选就把价钱减掉
        } else {
            CheckBox box = (CheckBox) buttonView;
            int str = Integer.parseInt(box.getText().toString());
            sum -= str;
            String sum1 = Integer.toString(sum);
            textView.setText(sum1);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                Toast.makeText(MainActivity.this, "一共:" + sum + "元", Toast.LENGTH_SHORT).show();
                break;
            //全选按钮
            case R.id.checkbox4:
                if (checkBox4.isChecked()) {
                    //获得全选按钮的状态
                    boolean bl = checkBox4.isChecked();
                    //设置列表CheckBox的状态,跟全选按钮状态一样
                    //说明:如果全选按钮被选中,所有按钮都会被选中
                    checkBox1.setChecked(bl);
                    checkBox2.setChecked(bl);
                    checkBox3.setChecked(bl);
                } else {
                    boolean bl = checkBox4.isChecked();
                    checkBox1.setChecked(bl);
                    checkBox2.setChecked(bl);
                    checkBox3.setChecked(bl);
                }
                break;
        }
    }
}

activity_main 布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!--第一个CheckBox-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <CheckBox
            android:id="@+id/checkbox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="682" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="元"
            android:textSize="15dp" />
    </LinearLayout>
    <!--第二个CheckBox-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <CheckBox
            android:id="@+id/checkbox2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="276" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="元"
            android:textSize="15dp" />
    </LinearLayout>
    <!--第三个CheckBox-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <CheckBox
            android:id="@+id/checkbox3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="66" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="元"
            android:textSize="15dp" />
    </LinearLayout>
    <!--全选-->
    <CheckBox
        android:id="@+id/checkbox4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="全选" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="0"
            android:textSize="50dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_toRightOf="@+id/tv"
            android:text="元"
            android:textSize="50dp" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:text="结算" />
    </RelativeLayout>
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值