Android數據存儲(一):SharedPreferences

SharedPreferences提供存儲一些簡單的配置信息機制,可以存儲一些用戶登錄名及密

碼,以鍵值對的方式存儲,下面介紹存儲用戶和密碼,退出程序或返回時再次打開應用程

序 ,仍能顯示原來用戶輸入的信息。SharedPreferences存儲了用戶輸入的信息,並顯示在界面上。

 

1.佈局文件:輸入用戶名及密碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.andrengroid.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     >
                                        
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="用戶名"
         tools:context=".MainActivity" />
                                        
     <EditText
         android:id="@+id/name"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:inputType="text"
         android:ems="10" >
     </EditText>
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="密碼"
         />
     <EditText
         android:id="@+id/password"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:ems="10"
         android:inputType="textPassword" />
                                        
 </LinearLayout>

2.編寫activity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class MainActivity extends Activity {
    private EditText name;
    private EditText pass;
    // String SAVEINFOS;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = (EditText) this.findViewById(R.id.name);

        pass = (EditText) this.findViewById(R.id.password);

        SharedPreferences sharedPreferences = getSharedPreferences("logininfo",
                MODE_PRIVATE);
        // 取出文件內存儲的值
        String name1 = sharedPreferences.getString("name", "");
        String password = sharedPreferences.getString("pass", "");
        if (name1 != null && !"".equals(name1)) {
            name.setText(name1);
        }
        if (password != null && !"".equals(password)) {
            pass.setText(password);
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    @Override
    protected void onStop() {
        super.onStop();
        // 應用程序退出時調用stop方法,此方法內保存輸入的參數
        String nametext = name.getText().toString();
        String passtext = pass.getText().toString();
        // 將參數存儲到logininfo文件內
        SharedPreferences share = getSharedPreferences("logininfo",
                Context.MODE_PRIVATE);
        // 得到編輯器,存儲參數值
        Editor editor = share.edit();
        editor.putString("name", nametext);
        editor.putString("pass", passtext);
        editor.commit();
    }
 }

3.演示效果



退出程序再打開


 

生成的文件logininfo在DDMSdata/data/項目名/shared_pres下,導出是個xml格式的文件




logininfo。xml文件內容

1
2
3
4
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
 <map> <string name="name">夏普</string>
      <string name="pass">123456</string>
 </map>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章