第三个Sprint ------第七天

APP.java代码

package com.app.senior_calculator;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class APP extends Activity {

    // 偏好设置
    //private SharedPreferences sp;
    //private SharedPreferences.Editor editor;

    /** level and mode (index) **/
    int selectedLevelIndex = 0;
    int selectedModeIndex = 0;
    int chioceItems;
    Bundle bundle = new Bundle();

    /** choices **/
    private String iconName1 = "选择难度";
    private String[] arrayLevel = new String[] { "加减", "乘除", "四则运算(无括号)",
            "四则运算(含括号)" };
    private String iconName2 = "选择模式";
    private String[] arrayModel = new String[] { "Practice", "Examination" };
    /**  题目库   **/
    private List<Question> source = new ArrayList<Question>();

    public void onclick(View v) {
        switch (v.getId()) {
        case R.id.StartTest:
            DialogDifficulty(arrayLevel, iconName1).show();
            break;
        case R.id.Medol:
            DialogDifficulty(arrayModel, iconName2).show();
            break;
        case R.id.Help:
            Toast.makeText(APP.this, " Dont know what to do……",
                    Toast.LENGTH_SHORT).show();
            break;
        case R.id.Exit:
            Toast.makeText(APP.this, " Closing…………", Toast.LENGTH_SHORT).show();
            finish();
            break;
        }
    }

    /** 定义一个Dialog 展示 level选择 后者为标题 **/
    public Dialog DialogDifficulty(final String[] array_data,
            final String dialogtitle) {
        chioceItems = selectedLevelIndex;
        if (dialogtitle.equals("选择难度"))
            chioceItems = selectedLevelIndex;
        else if (dialogtitle.equals("选择模式"))
            chioceItems = selectedModeIndex;

        Dialog alertDialog;
        alertDialog = new AlertDialog.Builder(this, R.style.dialog)
                .setTitle(dialogtitle)
                .setIcon(R.drawable.diologicon)
                .setSingleChoiceItems(array_data, chioceItems,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                if (dialogtitle.equals("选择难度"))
                                    selectedLevelIndex = which;
                                else if (dialogtitle.equals("选择模式"))
                                    selectedModeIndex = which;
                                /** 模式传递过去TestView根据模式来设置时间长度. */
                                bundle.putString("difficulty",
                                        arrayModel[selectedModeIndex]);
                            }
                        })
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        bundle.putString("difficulty",
                                arrayModel[selectedModeIndex]);
/*
                        // 确定后保存数据模式跟难度的下标数据..
                        if (dialogtitle.equals("选择难度"))                        
                            editor.putInt("selectedLevelIndex", which);
                    
                        if (dialogtitle.equals("选择模式"))
                            editor.putInt("selectedModeIndex", which);
                         
                        editor.commit();// 提交修改
*/
                        /** 在确定难度后跳转出题目 设置模式到时候不跳转. **/
                        if (dialogtitle.equals("选择难度"))
                            createexercise();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 实现函数- 。- 调用.
                        Toast.makeText(APP.this, " canceling ",
                                Toast.LENGTH_SHORT).show();
                    }
                }).create();
        return alertDialog;
    }

    /** 出题模块 根据选择的模式出先不同数量的题目. **/
    public void createexercise() {
        int totalNumber = 50;
        if (arrayModel[selectedModeIndex].equals("Examination"))
            totalNumber = 25;
        switch (selectedLevelIndex) {
        case 0:
            source = new ArrayList<Question>();
            Toast.makeText(APP.this, String.valueOf(selectedLevelIndex), Toast.LENGTH_LONG)
                    .show();
            new SimpleCreate().simpleExerciseInitation(0, totalNumber, source);
            for (int i = 0; i < source.size(); i++)
                Log.i("info", source.get(i).getExercise() + "="
                        + source.get(i).getAnswer());
            bundle.putSerializable("resource", (Serializable) source);
            Intent MAintent = new Intent(APP.this, TestView.class);
            MAintent.putExtras(bundle);
            startActivity(MAintent);
            break;
        case 1:
            source = new ArrayList<Question>();
            new SimpleCreate().simpleExerciseInitation(1, totalNumber, source);
            for (int i = 0; i < source.size(); i++)
                Log.i("info", source.get(i).getExercise() + "="
                        + source.get(i).getAnswer());
            bundle.putSerializable("resource", (Serializable) source);
            Intent intent1 = new Intent(APP.this, TestView.class);
            intent1.putExtras(bundle);
            startActivity(intent1);
            break;

        case 2:
            source = new ArrayList<Question>();
            new MidiumCreate().midiumCreateInitation(totalNumber, source);
            for (int i = 0; i < source.size(); i++)
                Log.i("info", source.get(i).getExercise() + "="
                        + source.get(i).getAnswer());
            bundle.putSerializable("resource", (Serializable) source);
            Intent intent2 = new Intent(APP.this, TestView.class);
            intent2.putExtras(bundle);
            startActivity(intent2);
            break;
        case 3:
            source = new ArrayList<Question>();
            // 出题目验证.    
            new CreateEFraction().createYouExercisess(totalNumber, source);
            for (int i = 0; i < source.size(); i++)
                Log.i("info", source.get(i).getExercise() + "="
                        + source.get(i).getAnswer());
            bundle.putSerializable("resource", (Serializable) source);
            Intent intent3 = new Intent(APP.this, TestView.class);
            intent3.putExtras(bundle);
            startActivity(intent3);
            break;
        default:
            Toast.makeText(APP.this, "errors happend ", Toast.LENGTH_LONG)
                    .show();
            break;
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app);
        /*// 获取偏好设置实例
        sp = getSharedPreferences("MySP", MODE_PRIVATE);
        editor = sp.edit();
     editor.putInt("selectedLevelIndex",0);
        editor.putInt("selectedModeIndex", 0);
        editor.commit();// 提交修改
*/     }

}
原文地址:https://www.cnblogs.com/kangqu/p/5043386.html