消費劵採購列表(多選項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);

    }

 

}

 

邏輯有待添加。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章