消费劵采购列表(多选项CheckBox的应用)

消费劵采购列表(多选项CheckBox的应用)

新建一个继承Activity类的MultiCheckBoxActivity,并设置布局文件为:multicheckbox.xml。

首先在布局文件中添加一个TextView和3个CheckBox组件。

  <TextView

        android:id="@+id/multicheckbox_tv01"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center_horizontal"

        android:textSize="20sp" />

 

    <CheckBox

        android:id="@+id/multicheckbox_checkbox01"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:checked="false"

        android:text="@string/apple"

        android:textSize="18sp" />

 

    <CheckBox

        android:id="@+id/multicheckbox_checkbox02"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:checked="false"

        android:text="@string/pear"

        android:textSize="18sp" />

 

    <CheckBox

        android:id="@+id/multicheckbox_checkbox03"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:checked="false"

        android:text="@string/orange"

        android:textSize="18sp" />

在Activity代码中获取这4个组件。并设置CheckBox的

package lyx.feng.second;

 

import lyx.feng.simpletextdemo.R;

import android.app.Activity;

import android.os.Bundle;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.CompoundButton.OnCheckedChangeListener;

import android.widget.TextView;

 

public class MultiCheckBoxActivity extends Activity implements

       OnCheckedChangeListener {

    private TextView tv = null;

    private CheckBox checkBox01 = null;

    private CheckBox checkBox02 = null;

    private CheckBox checkBox03 = null;

    private int totalMoney = 10;

    private String info = "";

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       super.setContentView(R.layout.multicheckbox);

       this.tv = (TextView) super.findViewById(R.id.multicheckbox_tv01);

       this.checkBox01 = (CheckBox) super

              .findViewById(R.id.multicheckbox_checkbox01);

       this.checkBox02 = (CheckBox) super

              .findViewById(R.id.multicheckbox_checkbox02);

       this.checkBox03 = (CheckBox) super

              .findViewById(R.id.multicheckbox_checkbox03);

 

       this.info = "你有" + totalMoney + "\n请选择你要买的东西:";

       this.tv.setText(info);

       this.info = "你买了:\n";

       this.checkBox01.setOnCheckedChangeListener(this);

       this.checkBox02.setOnCheckedChangeListener(this);

       this.checkBox03.setOnCheckedChangeListener(this);

    }

 

    @Override

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

       switch (buttonView.getId()) {

       case R.id.multicheckbox_checkbox01:

           if (isChecked) {

              // Apple 5

              this.totalMoney = this.totalMoney - 5;

              if (!info.contains("Apple")) {

                  info = info + "Apple\n";

              }

 

           } else {

              this.totalMoney = this.totalMoney + 5;

 

              info.replaceAll("Apple", "");

           }

           break;

       case R.id.multicheckbox_checkbox02:

           if (isChecked) {

              // Pear 3

              this.totalMoney = this.totalMoney - 3;

              if (!info.contains("Pear")) {

                  info = info + "Pear\n";

              }

           } else {

              this.totalMoney = this.totalMoney + 3;

              info.replaceAll("Pear", "");

           }

           break;

       case R.id.multicheckbox_checkbox03:

           if (isChecked) {

              // Orange 1

              this.totalMoney = this.totalMoney - 1;

              if (!info.contains("Orange")) {

                  info = info + "Orange\n";

              }

           } else {

              this.totalMoney = this.totalMoney + 1;

              info.replace("Orange", "");

           }

           break;

       }

       if (totalMoney == 10) {

           this.info = "请选择你要买的东西:";

       }

       this.tv.setText("你有" + totalMoney + "" + info);

    }

 

}

 

逻辑有待添加。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值