简体   繁体   English

SharedPreferences和TimePickerDialog

[英]SharedPreferences and TimePickerDialog

Hurro! 哈罗!
I'm trying my hand at writing my first Android app, and I've hit a brick wall. 我正在尝试编写自己的第一个Android应用程序,但遇到了麻烦。

I want to allow the user to set a 'start' and a 'stop' time, for a daily schedule. 我想允许用户为每日时间表设置“开始”和“停止”时间。 I have sucessfully implemented the TimePickerDialog, and the TextView next to it, but I can't for the LIFE of me figure out how to save the user selected time to a SharedPreferences string. 我已经成功实现了TimePickerDialog和它旁边的TextView,但是我不能为我的生活弄清楚如何将用户选择的时间保存到SharedPreferences字符串中。

Code is as follows (stripped down for clarity's sake): 代码如下(为清晰起见,将其分解):

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setup);
        // Retrieve the shared preferences
        mUserSettings = getSharedPreferences(USER_PREFERENCES, MODE_PRIVATE);

        // capture our View elements
        mTimeDisplayStart = (TextView)  findViewById(R.id.TextView_ScheduleStart_Info);
        mPickTimeStart = (Button) findViewById(R.id.Button_ScheduleStart);

        // add a click listener to the button
        mPickTimeStart.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(TIME_DIALOG_START);
            }
        });

        // get the current time
        final Calendar c = Calendar.getInstance();
        mHourStart = c.get(Calendar.HOUR_OF_DAY);
        mMinuteStart = c.get(Calendar.MINUTE);

        // display the current date
        updateDisplayStart();
    }

    // updates the time we display in the TextView
    private void updateDisplayStart() {
        mTimeDisplayStart.setText(
                new StringBuilder()
                .append(padStart(mHourStart)).append(":")
                .append(padStart(mMinuteStart)));
    }

    private static String padStart(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }

    // the callback received when the user "sets" the time in the dialog
    private TimePickerDialog.OnTimeSetListener mTimeSetListenerStart =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHourStart = hourOfDay;
                mMinuteStart = minute;
                updateDisplayStart();
            }
        };

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case TIME_DIALOG_START:
            return new TimePickerDialog(this,
                    mTimeSetListenerStart, mHourStart, mMinuteStart, false);
        }
        return null;
    }

I assume that the time should be saved at the same time that the TextView is updated... but I have no idea how to do this -.- 我认为应该在TextView更新的同时保存时间...但是我不知道该怎么做-.-

I've now spent a good 5 or 6 hours looking for an answer, but I literally can't find one. 现在,我已经花了5到6个小时来寻找答案,但是我却找不到答案。 I've experimented with the SharedPreferences.Editor, but that won't work because I need to store the time as a long, not a String. 我已经尝试了SharedPreferences.Editor,但是那行不通,因为我需要将时间存储为很长的时间,而不是字符串。
I'm at the end of my rope, and I'd really appreciate some help!! 我已经走到尽头了,非常感谢您的帮助!!

in the following method 在以下方法中

    // updates the time we display in the TextView
         private void updateDisplayStart() {
            mTimeDisplayStart.setText(
                    new StringBuilder()
                    .append(padStart(mHourStart)).append(":")
                    .append(padStart(mMinuteStart)));
        });

     SharedPreferences settingsActivity = getSharedPreferences("todolist1",
                    Activity.MODE_PRIVATE);
            SharedPreferences.Editor prefEditor = settingsActivity.edit();

            prefEditor.putString("DateToDisplay",new StringBuilder()
                    .append(padStart(mHourStart)).append(":")
                    .append(padStart(mMinuteStart));  // updates the time we display in the TextView



        prefEditor.commit();
}

After this where ever u want retrieve the data from the shared preference as follows 之后,您要从共享首选项中检索数据,如下所示

SharedPreferences settingsActivity = getSharedPreferences("todolist1",
                Activity.MODE_PRIVATE);

        if (settingsActivity.contains(DateToDisplay)) {
            String saveddate = settingsActivity
                    .getString(DateToDisplay, "");

You have to do like this 你必须这样

private TimePickerDialog.OnTimeSetListener mTimeSetListenerStart =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHourStart = hourOfDay;
                mMinuteStart = minute;
                updateDisplayStart();

                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
                cal.set(Calendar.MINUTE, minute);
                mUserSettings.edit().putLong("starttime", cal.getTimeInMillis()).commit();

            }
        };

SharedPreferences supports long s. SharedPreferences支持long You can do something like 你可以做类似的事情

SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE); prefs.edit().putLong("longKey", longValue).commit();

to store the value. 存储值。 To retrieve it, you can do 要检索它,您可以执行

SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE); long longValue = prefs.getLong("longKey", 0l);

this one code for edit to SharedPreference file :- 此代码用于编辑SharedPreference文件:-

  1. private SharedPreferences myPrefs; 私人SharedPreferences myPrefs; myPrefs = this.getSharedPreferences("filename",MODE_WORLD_WRITEABLE); myPrefs = this.getSharedPreferences(“ filename”,MODE_WORLD_WRITEABLE); SharedPreferences.Editor prefsEditor = myPrefs.edit(); SharedPreferences.Editor prefsEditor = myPrefs.edit(); prefsEditor.putString("Mobile_no", getText_no.getText().toString().trim()); prefsEditor.putString(“ Mobile_no”,getText_no.getText()。toString()。trim()); prefsEditor.commit();` prefsEditor.commit();`

this one code for get to values SharedPreference file :- 此代码获取值SharedPreference文件:-

  1. myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE); myPrefs = this.getSharedPreferences(“ myPrefs”,MODE_WORLD_READABLE); String X = myPrefs.getString("Mobile_no", ""); 字符串X = myPrefs.getString(“ Mobile_no”,“”);

X == get information X ==获取信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM