Android材质对话框和存储选定值

时间:2016-10-27 05:55:41

标签: android sharedpreferences android-alertdialog

我的申请与闹钟有关。我在我的应用程序中使用了材料AlertDialog。我从AlertDialog中选择一个值,然后按选择按钮。我想存储选定的值。它值集,直到我没有改变它。我怎么样?

final Dialog dialog = new MaterialDialog.Builder(Settings_Activity.this)
                .title(R.string.full_battery_alarm1)
                .iconRes(R.mipmap.ic_launcher)
                .limitIconToDefaultSize()
                .items(R.array.full_battery_alarm)
                .itemsCallbackSingleChoice(0, new MaterialDialog.ListCallbackSingleChoice() {
                    @Override
                    public boolean onSelection(MaterialDialog mdialog, View view, int pos, CharSequence text) {
                        manager.sessionWork(pos);

                        showToast(pos + " = " + text);
                        int i = Integer.parseInt(text.toString());

                        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(Settings_Activity.this);
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.putString("select full", Integer.toString(i));
                        editor.apply();

                        String abc = preferences.getString("select full", null);

                        final int l = Integer.parseInt(abc);

                        BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
                            public void onReceive(Context context, Intent intent) {
                                context.unregisterReceiver(this);
                                int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

                                if (rawlevel >= 0 && scale > 0) {
                                    level = (rawlevel * 100) / scale;
                                    showToast("level full" + level);


                                    if (level == l) {
                                        showToast("level full 2"+level);

                                        Intent i1 = new Intent(Settings_Activity.this, StopActivity.class);
                                        i1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                        startActivity(i1);
                                        finish();
                                    }
                                }

                            }
                        };
                        IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
                        registerReceiver(batteryLevelReceiver, batteryLevelFilter);
                        mdialog.setSelectedIndex(pos); 
                        return true;
                    }
                })
                .positiveText(R.string.choose)
                .show();

2 个答案:

答案 0 :(得分:0)

将其存储在共享首选项中,或者只使用db line sql lite




答案 1 :(得分:0)

您可以像这样使用SharedPreferences。创建您的SharedPreferences和Editor。

static SharedPreferences sharedData;
static SharedPreferences.Editor editor;

现在

sharedData = getSharedPreferences("mySharedPreference", Context.MODE_PRIVATE);
editor = sharedData.edit();

之后保存你的价值

editor.putString("valueKey", ""+yourvaluevariable); editor.commit();

现在这个值已保存,并且在关闭它之后稍后启动应用程序时将检索该事件。 要在之后访问该值,请使用

sharedData.getString("valueKey", "");