我如何获得这些值来保存?

时间:2011-07-16 04:02:51

标签: java android

我需要在退出应用程序后显示此edittext中的插入值。我目前已将其设置为如果这是用户第一次设置设置页面,它将设置新的默认值,但我无法获取设置值以保存和加载相同的值。这是我的代码。

            if (res.equals("555")) {
            //post the saved text
        } else if (res.equals("510")) {
            editTextname.setText("Firstname Lastname", TextView.BufferType.EDITABLE);
            editTextphone.setText("XXX-XXX-XXXX", TextView.BufferType.EDITABLE);
            editTextemail.setText("name@yourdomain.com", TextView.BufferType.EDITABLE);
            editTextaddress.setText("Street, City, State, Zip", TextView.BufferType.EDITABLE);
            //save the entered text above and show it
        }

1 个答案:

答案 0 :(得分:1)

使用SharedPreferences

SharedPreferences prefs = getSharedPreferences("MyPreference", MODE_PRIVATE);

// On first run:
SharedPreferences.Editor editor = prefs.edit();
if(!prefs.contains("key1"))
    editor.putString("key1", "defaultValue1");
if(!prefs.contains("key2"))
    editor.putString("key2", "defaultValue2");
editor.commit();

// On save:
SharedPreferences.Editor editor = prefs.edit();
editor.putString("key1", "value1");
editor.putString("key2", "value3");
editor.commit();

// On read:
String value1 = prefs.getString("key1");
String value2 = prefs.getString("key2");