日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

(转)Android SharedPreferences的使用

發布時間:2025/4/5 Android 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (转)Android SharedPreferences的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文連接:http://blog.csdn.net/shang_515/article/details/6884635

?

SharedPreferences是Android平臺上一個輕量級的存儲類,主要是保存一些常用的配置比如窗口狀態,一般在Activity中 重載窗口狀態onSaveInstanceState保存一般使用SharedPreferences完成,它提供了Android平臺常規的Long長 整形、Int整形、String字符串型的保存,它是什么樣的處理方式呢?SharedPreferences類似過去Windows系統上的ini配置文件,但是它分為多種權限,可以全局共享訪問,android123提示最 終是以xml方式來保存,整體效率來看不是特別的高,對于常規的輕量級而言比SQLite要好不少,如果真的存儲量不大可以考慮自己定義文件格式。xml 處理時Dalvik會通過自帶底層的本地XML Parser解析,比如XMLpull方式,這樣對于內存資源占用比較好。

?

這種方式應該是用起來最簡單的Android讀寫外部數據的方法了。他的用法基本上和 J2SE(java.util.prefs.Preferences)中的用法一樣,以一種簡單、 透明的方式來保存一些用戶個性化設置的字體、顏色、位置等參數信息。一般的應用程序都會提供“設置”或者“首選項”的這樣的界面,那么這些設置最后就可以 通過Preferences來保存,而程序員不需要知道它到底以什么形式保存的,保存在了什么地方。當然,如果你愿意保存其他的東西,也沒有什么限制。只 是在性能上不知道會有什么問題。

在Android系統中,這些信息以XML文件的形式保存在?/data/data/PACKAGE_NAME?/shared_prefs?目錄下。

?

下面是程序代碼:

?

[java]?view plaincopy
  • package?com.cloay;??
  • ??
  • import?android.app.Activity;??
  • import?android.content.SharedPreferences;??
  • import?android.os.Bundle;??
  • import?android.view.MotionEvent;??
  • import?android.view.View;??
  • import?android.view.View.OnTouchListener;??
  • import?android.widget.EditText;??
  • import?android.widget.ImageButton;??
  • /**?
  • ?*??
  • ?*?MySharedPreferencesActivity.java?
  • ?*?@author?cloay?
  • ?*?2011-10-18?
  • ?*/??
  • public?class?MySharedPreferencesActivity?extends?Activity?{??
  • ????private?EditText?user?=?null;??
  • ????private?EditText?password?=?null;??
  • ??????
  • ????private?ImageButton?loginBtn?=?null;??
  • ????@Override??
  • ????public?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.main);??
  • ????????user?=?(EditText)findViewById(R.id.user);??
  • ????????password?=?(EditText)findViewById(R.id.pass);??
  • ????????loginBtn?=?(ImageButton)findViewById(R.id.loginButton);??
  • ????????initView();??
  • ????????loginBtn.setOnTouchListener(new?OnTouchListener(){??
  • ??
  • ????????????@Override??
  • ????????????public?boolean?onTouch(View?v,?MotionEvent?event)?{??
  • ????????????????if(event.getAction()==MotionEvent.ACTION_DOWN){??
  • ????????????????????v.setBackgroundResource(R.drawable.dengluxitong1);??
  • ????????????????????SharedPreferences?userInfo?=?getSharedPreferences("user_info",?0);??
  • ????????????????????userInfo.edit().putString("name",?user.getText().toString()).commit();??
  • ????????????????????userInfo.edit().putString("pass",?password.getText().toString()).commit();??
  • ????????????????}??
  • ????????????????else?if(event.getAction()==MotionEvent.ACTION_UP){??
  • ????????????????????v.setBackgroundResource(R.drawable.dengluxitong);??
  • ????????????????}??
  • ????????????????return?false;??
  • ????????????}??
  • ??????????????
  • ????????});??
  • ????}??
  • ????private?void?initView()?{??
  • ????????SharedPreferences?userInfo?=?getSharedPreferences("user_info",?0);??
  • ????????String?username?=?userInfo.getString("name",?"");??
  • ????????String?pass?=?userInfo.getString("pass",?"");??
  • ????????user.setText(username);??
  • ????????password.setText(pass);??
  • ????}??
  • }??

  • SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    通過名稱,得到一個SharedPreferences,顧名思義,這個Preferences是共享的,共享的范圍據現在同一個Package中,這里 面說所的Package和Java里面的那個Package不同,貌似這里面的Package是指在AndroidManifest.xml文件中的

    ?

    ?

    [html]?view plaincopy
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <manifest?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ??????package="com.cloay"??
  • ??????android:versionCode="1"??
  • ??????android:versionName="1.0">??
  • ????<uses-sdk?android:minSdkVersion="8"?/>??
  • ??
  • ????<application?android:icon="@drawable/icon"?android:label="@string/app_name">??
  • ????????<activity?android:name=".MySharedPreferencesActivity"??
  • ??????????????????android:label="@string/app_name">??
  • ????????????<intent-filter>??
  • ????????????????<action?android:name="android.intent.action.MAIN"?/>??
  • ????????????????<category?android:name="android.intent.category.LAUNCHER"?/>??
  • ????????????</intent-filter>??
  • ????????</activity>??
  • ??
  • ????</application>??
  • </manifest>??

  • 布局文件如下:

    ?

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <EditText android:layout_width="185dp" android:id="@+id/user"
    android:layout_height="40dp" android:hint="請輸入用戶名"
    android:singleLine="true" android:layout_alignParentTop="true"
    android:layout_alignLeft="@+id/pass" android:layout_marginTop="66dp">
    <requestFocus></requestFocus>
    </EditText>
    <EditText android:inputType="textPassword"
    android:layout_width="185dp" android:id="@+id/pass"
    android:layout_height="40dp" android:hint="請輸入密碼" android:singleLine="true"
    android:layout_below="@+id/user" android:layout_centerHorizontal="true"
    android:layout_marginTop="44dp">
    </EditText>
    <ImageButton android:layout_height="40dp"
    android:layout_width="80dp" android:id="@+id/loginButton"
    android:background="@drawable/dengluxitong"
    android:layout_centerVertical="true" android:layout_alignRight="@+id/pass"
    android:layout_marginRight="17dp"></ImageButton>
    </RelativeLayout>

    運行結果如下,首次顯示的時空白,第二次運行時如下:

    轉載于:https://www.cnblogs.com/lovers/articles/2679853.html

    總結

    以上是生活随笔為你收集整理的(转)Android SharedPreferences的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。