【Android学习笔记】SharedPreferences & AutoCompleteTextView

    四年时间如白驹过隙,转眼间,我也站在了毕业季的路口。过去的一年,从找实习到实习;再从找工作到工作。经历的种种,学习到的种种,应该是大学四年中比较宝贵的财富了。

  也由于这些事情,我总是匆忙的做着别人告诉我,我应该做的事情,所以博客写得少了。现在定下心来,我该做自己认为应该做的事情了。现实也许不会总是允许我们这样做。但时间嘛,就像乳沟,挤挤总是有的。

  回顾这四年,只剩一句感叹“还好,四年没有完全荒废。”

  最终工作签到了Android,虽然和以前学习的.Net似乎没有半点关系,而且开发环境和语言都改成了Java的那一套。我还是很庆幸有.Net的基础,帮助我学习Android。

今天记录一下SharedPreferences & AutoCompleteTextView,以后还有更多内容推出,帮助自己记录的同时,也希望可以帮到和我一样的Android新手。

  在此诚挚的感谢各位写技术博客的同志们,是你们让我在无从下手的时候找到了出路。顺道感谢Google 和  百毒。 

PS:学习Android,用好官方文档和百毒、Google,你就无敌了。-_-||

 

  SharedPreferences是以键值对来存储应用程序的配置信息的一种方式,它只能存储基本数据类型。一个程序的配置文件仅可以在本应用程序中使用,或者说只能在同一个包内使用,不能在不同的包之间使用。 实际上SharedPreferences是采用了XML格式将数据存储到设备中,在DDMS中的File Explorer中的/data/data/<package name>/shares_prefs下。

以下表格为获取SharedPreferences对象的两个方法:

返回值

函数

备注

SharedPreferences

Context.getSharedPreferences(String name,int mode)

name为本组件的配置文件名(如果想要与本应用程序的其他组件共享此配置文件,可以用这个名字来检索到这个配置文件)。

mode为操作模式,默认的模式为0或MODE_PRIVATE,还可以使用MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE。

SharedPreferences

Activity.getPreferences(int mode)

配置文件仅可以被调用的Activity使用。

mode为操作模式,默认的模式为0或MODE_PRIVATE,还可以使用MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE。

  如果要读取配置文件信息,只需要直接使用SharedPreferences对象的getXXX()方法即可,而如果要写入配置信息,则必须先调用SharedPreferences对象的edit()方法,使其处于可编辑状态,然后再调用putXXX()方法写入配置信息,最后调用commit()方法提交更改后的配置文件。

以下为示例代码,功能记录用户输入的用户名和密码,下次使用时可以不用输入;QQ栏可以记录用户输入过的QQ号,再次输入时可以自动提示。

注释写得比较详细,不再多说。

主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 * 保存数据到SharedPreferences
47 * */
48 private void savePreferences() {
49 String qQString;//AutoCompleteTextView控件中所需要的Adapter数据
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())) {//保证不会有重复的item存入
55 qQString += "#" + autoCompleteTextView.getText().toString();//每个Item以#号分隔;
56 //SharedPreferences只能保存基本数据类型,所以此处把Items保存为String,然后再解析成item数组
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读取数据
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);//设置AutoCompleteTextView的内容
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>
posted @ 2011-04-06 13:25  Cat_Lee  阅读(2257)  评论(1编辑  收藏  举报