<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      【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>
      posted @ 2011-04-06 13:25  Cat_Lee  閱讀(2271)  評(píng)論(1)    收藏  舉報(bào)
      主站蜘蛛池模板: 亚洲综合av一区二区三区| 柯坪县| 亚洲国产精品高清线久久| 人妻激情文学| 亚洲精品国产免费av| 亚洲一本二区偷拍精品| 波多野结系列18部无码观看AV| 激情伊人五月天久久综合| 午夜男女爽爽影院免费视频下载| 年轻女教师hd中字3| 香蕉久久国产精品免| 日韩人妻无码一区二区三区99 | 国内精品大秀视频日韩精品| 玩弄放荡人妻少妇系列| 精品国产粉嫩内射白浆内射双马尾| 久久综合色最新久久综合色| 亚洲香蕉免费有线视频| 中文字幕在线亚洲精品| 91老熟女老人国产老太| 中文字幕人妻丝袜美腿乱| 久久99精品久久99日本| 国产一区二区三区精品综合| 又爽又黄又无遮挡的激情视频| 日韩一区二区三区日韩精品| 成熟少妇XXXXX高清视频| 久久亚洲国产精品久久| 国产最新AV在线播放不卡| 婷婷开心深爱五月天播播| 亚洲一区二区中文字幕| 日韩无专区精品中文字幕| 丁香五月婷激情综合第九色| 99riav国产精品视频| 国产漂亮白嫩美女在线观看| 久久久这里只有精品10| 精品乱人伦一区二区三区| 日韩激情无码免费毛片| 国产精品人成在线观看免费| 无码AV无码免费一区二区| 国产999久久高清免费观看| 性猛交ⅹxxx富婆视频| 四虎永久地址www成人|