android对话框,checkBox,同时在同一个页面上保存数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android对话框,checkBox,同时在同一个页面上保存数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3713字,纯文字阅读大概需要6分钟。
内容图文
package com.example.selectonlyonle;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>
原文:http://blog.csdn.net/yangjianbo456/article/details/40409323
内容总结
以上是互联网集市为您收集整理的android对话框,checkBox,同时在同一个页面上保存数据全部内容,希望文章能够帮你解决android对话框,checkBox,同时在同一个页面上保存数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。