您的位置:首页 > 移动开发 > Android开发

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

2016-03-03 21:57 573 查看

消费劵采购列表(多选项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);
    }
 
}
 

逻辑有待添加。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息