消费劵采购列表(多选项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" /> |
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); } } |
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories