【Android學(xué)習(xí)筆記】SharedPreferences & AutoCompleteTextView
四年時(shí)間如白駒過(guò)隙,轉(zhuǎn)眼間,我也站在了畢業(yè)季的路口。過(guò)去的一年,從找實(shí)習(xí)到實(shí)習(xí);再?gòu)恼夜ぷ鞯焦ぷ鳌=?jīng)歷的種種,學(xué)習(xí)到的種種,應(yīng)該是大學(xué)四年中比較寶貴的財(cái)富了。
也由于這些事情,我總是匆忙的做著別人告訴我,我應(yīng)該做的事情,所以博客寫得少了。現(xiàn)在定下心來(lái),我該做自己認(rèn)為應(yīng)該做的事情了。現(xiàn)實(shí)也許不會(huì)總是允許我們這樣做。但時(shí)間嘛,就像乳溝,擠擠總是有的。
回顧這四年,只剩一句感嘆“還好,四年沒有完全荒廢。”
最終工作簽到了Android,雖然和以前學(xué)習(xí)的.Net似乎沒有半點(diǎn)關(guān)系,而且開發(fā)環(huán)境和語(yǔ)言都改成了Java的那一套。我還是很慶幸有.Net的基礎(chǔ),幫助我學(xué)習(xí)Android。
今天記錄一下SharedPreferences & AutoCompleteTextView,以后還有更多內(nèi)容推出,幫助自己記錄的同時(shí),也希望可以幫到和我一樣的Android新手。
在此誠(chéng)摯的感謝各位寫技術(shù)博客的同志們,是你們讓我在無(wú)從下手的時(shí)候找到了出路。順道感謝Google 和 百毒。
PS:學(xué)習(xí)Android,用好官方文檔和百毒、Google,你就無(wú)敵了。-_-||
SharedPreferences是以鍵值對(duì)來(lái)存儲(chǔ)應(yīng)用程序的配置信息的一種方式,它只能存儲(chǔ)基本數(shù)據(jù)類型。一個(gè)程序的配置文件僅可以在本應(yīng)用程序中使用,或者說(shuō)只能在同一個(gè)包內(nèi)使用,不能在不同的包之間使用。 實(shí)際上SharedPreferences是采用了XML格式將數(shù)據(jù)存儲(chǔ)到設(shè)備中,在DDMS中的File Explorer中的/data/data/<package name>/shares_prefs下。
以下表格為獲取SharedPreferences對(duì)象的兩個(gè)方法:
|
返回值 |
函數(shù) |
備注 |
|
SharedPreferences |
Context.getSharedPreferences(String name,int mode) |
name為本組件的配置文件名(如果想要與本應(yīng)用程序的其他組件共享此配置文件,可以用這個(gè)名字來(lái)檢索到這個(gè)配置文件)。 mode為操作模式,默認(rèn)的模式為0或MODE_PRIVATE,還可以使用MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE。 |
|
SharedPreferences |
Activity.getPreferences(int mode) |
配置文件僅可以被調(diào)用的Activity使用。 mode為操作模式,默認(rèn)的模式為0或MODE_PRIVATE,還可以使用MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE。 |
如果要讀取配置文件信息,只需要直接使用SharedPreferences對(duì)象的getXXX()方法即可,而如果要寫入配置信息,則必須先調(diào)用SharedPreferences對(duì)象的edit()方法,使其處于可編輯狀態(tài),然后再調(diào)用putXXX()方法寫入配置信息,最后調(diào)用commit()方法提交更改后的配置文件。
以下為示例代碼,功能記錄用戶輸入的用戶名和密碼,下次使用時(shí)可以不用輸入;QQ欄可以記錄用戶輸入過(guò)的QQ號(hào),再次輸入時(shí)可以自動(dòng)提示。
注釋寫得比較詳細(xì),不再多說(shuō)。
主Activity:
SharedPreferencesDemo
1 package com.mtk.lc.SharedPreferencesDemo;
2
3 import android.app.Activity;
4 import android.content.SharedPreferences;
5 import android.os.Bundle;
6 import android.view.View;
7 import android.widget.ArrayAdapter;
8 import android.widget.AutoCompleteTextView;
9 import android.widget.Button;
10 import android.widget.EditText;
11
12 public class SharedPreferencesDemo extends Activity {
13 private Button btnok;
14 private EditText editText1;
15 private EditText editText2;
16 private AutoCompleteTextView autoCompleteTextView;
17 public static final String PREFS_STRING = "MyPrefs";//SharedPreferences名字
18
19 /** Called when the activity is first created. */
20 @Override
21 public void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);
23 setContentView(R.layout.main);
24 getCompents();
25 getPreferences();
26 }
27
28 /**
29 * 獲取控件
30 * */
31 private void getCompents() {
32 btnok = (Button) findViewById(R.id.button1);
33 editText1 = (EditText) findViewById(R.id.editText1);
34 editText2 = (EditText) findViewById(R.id.editText2);
35 autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
36 btnok.setOnClickListener(new View.OnClickListener() {
37
38 @Override
39 public void onClick(View v) {
40 savePreferences();
41
42 }
43 });
44 }
45 /**
46 * 保存數(shù)據(jù)到SharedPreferences
47 * */
48 private void savePreferences() {
49 String qQString;//AutoCompleteTextView控件中所需要的Adapter數(shù)據(jù)
50 SharedPreferences settings = getSharedPreferences(PREFS_STRING,
51 MODE_PRIVATE);
52 SharedPreferences.Editor editor = settings.edit();
53 qQString = settings.getString("QQ", "");
54 if (!qQString.contains(autoCompleteTextView.getText().toString())) {//保證不會(huì)有重復(fù)的item存入
55 qQString += "#" + autoCompleteTextView.getText().toString();//每個(gè)Item以#號(hào)分隔;
56 //SharedPreferences只能保存基本數(shù)據(jù)類型,所以此處把Items保存為String,然后再解析成item數(shù)組
57 editor.putString("QQ", qQString);
58 }
59 editor.putString("Name", editText1.getText().toString());
60 editor.putString("Password", editText2.getText().toString());
61 editor.commit();//記得提交修改
62 }
63 /**
64 * 從SharedPreferences讀取數(shù)據(jù)
65 * */
66 private void getPreferences() {
67 String qQString;
68 try {
69 SharedPreferences settings = getSharedPreferences(PREFS_STRING,
70 MODE_PRIVATE);
71 qQString = settings.getString("QQ", "");
72 String[] qqStrings = qQString.split("#");//從讀出的String中解析出Items
73 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
74 android.R.layout.simple_dropdown_item_1line, qqStrings);
75 autoCompleteTextView.setAdapter(adapter);//設(shè)置AutoCompleteTextView的內(nèi)容
76 editText1.setText(settings.getString("Name", ""));
77 editText2.setText(settings.getString("Password", ""));
78 } catch (Exception e) {
79 // TODO: handle exception
80 }
81
82 }
83 }
Layout:
layout
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical" android:layout_width="fill_parent"
4 android:layout_height="fill_parent">
5 <TextView android:id="@+id/textView1" android:layout_width="wrap_content"
6 android:layout_height="wrap_content" android:text="Name"></TextView>
7 <EditText android:layout_width="match_parent" android:id="@+id/editText1"
8 android:layout_height="wrap_content"></EditText>
9 <TextView android:id="@+id/textView2" android:layout_width="wrap_content"
10 android:layout_height="wrap_content" android:text="Password"></TextView>
11 <EditText android:layout_width="match_parent" android:id="@+id/editText2"
12 android:layout_height="wrap_content"></EditText>
13 <TextView android:id="@+id/textView3" android:layout_width="wrap_content"
14 android:layout_height="wrap_content" android:text="QQ(Auto)"></TextView>
15 <AutoCompleteTextView android:layout_width="match_parent"
16 android:id="@+id/autoCompleteTextView1" android:layout_height="wrap_content"></AutoCompleteTextView>
17 <Button android:layout_width="wrap_content"
18 android:layout_height="wrap_content" android:id="@+id/button1"
19 android:text="OK"></Button>
20 </LinearLayout>
Finish Whatever U've Started !!!



浙公網(wǎng)安備 33010602011771號(hào)