首页 > 代码库 > android对话框,checkBox,同时在同一个页面上保存数据
android对话框,checkBox,同时在同一个页面上保存数据
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText editText;
private final static int DIALOG = 1;
boolean[] flags = new boolean[] { false, false, false, false };// 初始复选情况
String[] items = null;
private String lv_items[] = { "轮胎(400)", " 无骨雨刷(100)", "刹车片(235)",
"刹车盘(500)" };
private String lv_items_value[] = { "400", "100", "235", "500" };
private String result;
private int motoal;
private String test;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
items = getResources().getStringArray(R.array.hobby);
editText = (EditText) findViewById(R.id.editText);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 显示对话框
showDialog(DIALOG);
}
});
}
/**
* 创建复选框对话框
*/
@Override
public Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch (id) {
case DIALOG:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// 设置对话框的图标
// builder.setIcon(R.drawable.header);
// 设置对话框的标题
builder.setTitle("复选框对话框");
builder.setMultiChoiceItems(R.array.hobby, flags,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
flags[which] = isChecked;
System.out.println(which+"==which==="+which);
result = "您选择了:";
// for (int i = 0; i < flags.length; i++) {
if (flags[which]) {
result = result + items[which] + "、";
motoal = motoal
+ Integer
.valueOf(lv_items_value[which]);
System.out.println(result + "======"
+ motoal);
// test = test +lv_items_value[i];
}
// }
if (!flags[which]) {
// 如果选中了又不选了
motoal = motoal
- Integer
.valueOf(lv_items_value[which]);
System.out.println("==motoal=0="
+ motoal);
}
// for (int a = 0; a < 4; a++) {
// if (flags[a] == false) {
// motoal = motoal
// - Integer
// .valueOf(lv_items_value[a]);
// System.out.println("==motoal=0="
// + motoal);
// }
// }
// System.out.println("========="+test);
// editText.setText(motoal+"");
editText.setText(motoal + "");
}
});
// builder.setPositiveButton(result,
// new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog, int which) {
// }
// });
// 添加一个确定按钮
builder.setPositiveButton(" 确 定 ",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
// 创建一个复选框对话框
dialog = builder.create();
break;
}
return dialog;
}
}
----------------------array.xml---------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="hobby">
<item>轮胎(400)</item>
<item>无骨雨刷(100)</item>
<item>刹车片(235)</item>
<item>刹车盘(500)</item>
</string-array>
</resources>
这是看着网上的代码,只是稍加了自己的理解,以及checkbox中数据的加减
------------------------------activity_main.xml-----------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:editable="false"
android:text="" />
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示复选框对话框" />
</LinearLayout>
android对话框,checkBox,同时在同一个页面上保存数据