zoukankan      html  css  js  c++  java
  • android对话框,checkBox,同一时候在同一个页面上保存数据

    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) {
    // }
    // });
    // 加入一个确定button
    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>

  • 相关阅读:
    windwos8.1英文版安装SQL2008 R2中断停止的解决方案
    indwows8.1 英文版64位安装数据库时出现The ENU localization is not supported by this SQL Server media
    Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds
    SQL数据附加问题
    eclipse,myeclipse中集合svn的方法
    JAVA SSH 框架介绍
    SSH框架-相关知识点
    SuperMapRealSpace Heading Tilt Roll的理解
    SuperMap iserver manage不能访问本地目的(IE9)
    Myeclipse中js文件中的乱码处理
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/6740923.html
Copyright © 2011-2022 走看看