Is there a simple way to persistent input values of UI in Android?

时间:2015-08-07 01:54:34

标签: android

There are some editText controls in my UI, I hope these input values can be restored after I restart the UI, and more I need to use the saved values in my customize service.

I know I can use SharedPreferences to do that , but it's too complex if I have many controls. Is there a simple way?

I don't know if I can get saved value using savedInstanceState http://developer.android.com/training/basics/activity-lifecycle/recreating.html#SaveState

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText2"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

   private static String GetStringSharedPreferences(Context mContext,String key){
        SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(mContext);
        return prefs.getString(key, "");
    }

    private static void SaveStringSharedPreferences(Context mContext,String key,String input){
        SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(mContext);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(key, input);
        editor.commit();
    }

1 个答案:

答案 0 :(得分:1)

  1. Shared preferences is not at all difficult. Its simplest to use.
  2. You can try using AutoComplete feature of edittext. Refer below post. http://android.foxykeep.com/dev/how-to-add-autocompletion-to-an-edittext
  3. Refer this post for history Show a drop down list of recently entered text when clicks on an android edit box