最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android持久化技術(shù)之SharedPreferences存儲實例詳解

 更新時間:2016年01月15日 15:38:12   作者:殘缺的孤獨  
這篇文章主要介紹了Android持久化技術(shù)之SharedPreferences存儲,結(jié)合實例形式較為詳細的分析了SharedPreferences存儲的原理、應(yīng)用及具體實現(xiàn)方法,需要的朋友可以參考下

本文實例講述了Android持久化技術(shù)之SharedPreferences存儲。分享給大家供大家參考,具體如下:

1、SharedPreferences存儲

在前面一篇文章《Android持久化技術(shù)之文件的讀取與寫入實例詳解》中,我們介紹了Android持久化技術(shù)的文件的讀取與寫入。在本文中,繼續(xù)介紹Android持久化技術(shù)另外一個SharedPreferences存儲。

(1)SharedPreferences存儲方式是基于key-value的,通過key可以找到對應(yīng)的value。
(2)支持多種數(shù)據(jù)類型存儲,比如字符串、整形、布爾型等,并有對應(yīng)的存儲與獲取方法。
(3)獲取SharedPreferences對象有多種方式。
使用Context類的getSharedPreferences方法。
使用Activity類的getPreferences方法
使用PreferenceManager類的getDefaultSharedPreferences方法
(4)當存儲時,需要通過SharedPreferences對象獲取SharedPreferences.Editor對象
(5)默認存儲路徑為:/data/data/包名/shared_prefs/目錄
(6)存儲文件類型為xml文件

2、示例

場景:點擊保存按鈕,存儲數(shù)據(jù);點擊恢復(fù)按鈕,恢復(fù)數(shù)據(jù)。

(1)activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:stretchColumns="1"
  >
  <TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Account:" />
    <EditText
      android:id="@+id/account"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:hint="Input your account here"
      android:ems="10" >
    </EditText>
  </TableRow>
  <TableRow
    android:id="@+id/tableRow2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Password:"
       />
    <EditText
      android:id="@+id/password"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:ems="10"
      android:inputType="textPassword"
      >
    </EditText>
  </TableRow>
  <TableRow
    android:id="@+id/tableRow3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <Button
    android:id="@+id/login"
    android:layout_span="2"
    android:layout_height="wrap_content"
    android:text="save data" />
  </TableRow>
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="20dp"
    android:background="#ff0000"
    android:text="我是萬惡的分割線"
    android:textSize="20sp"
    android:gravity="center"
    />
   <TableRow
    android:id="@+id/tableRow4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Account:" />
    <EditText
      android:id="@+id/account2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:ems="10" >
    </EditText>
  </TableRow>
  <TableRow
    android:id="@+id/tableRow5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Password:"
       />
    <EditText
      android:id="@+id/password2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:ems="10"
      android:inputType="textPassword"
      >
    </EditText>
  </TableRow>
  <TableRow
    android:id="@+id/tableRow6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <Button
    android:id="@+id/login2"
    android:layout_span="2"
    android:layout_height="wrap_content"
    android:text="restore data" />
  </TableRow>
</TableLayout>

(2)MainActivity.java

package com.example.testsharedpreferences;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
 * Android 持久化技術(shù)-----SharedPreferences存儲
 * @author yy
 *
 */
public class MainActivity extends Activity {
  private EditText accountEdit;
  private EditText passwordEdit;
  private Button saveButton;
  private Button restoreButton;
  private SharedPreferences pref;
  private SharedPreferences.Editor editor;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //存儲按鈕
    saveButton = (Button) findViewById(R.id.login);
    //為存儲按鈕添加點擊事件
    saveButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
        //獲取SharedPreferences對象
        //第一個參數(shù):文件名,沒有則新建。第二個參數(shù):寫入模式-覆蓋
        pref = getSharedPreferences("second", MODE_PRIVATE);
        //獲取SharedPreferences.Editor對象
        editor = pref.edit();
        //獲取輸入的賬號內(nèi)容
        accountEdit = (EditText) findViewById(R.id.account);
        String account = accountEdit.getText().toString();
        //獲取輸入的密碼內(nèi)容
        passwordEdit = (EditText) findViewById(R.id.password);
        String password = passwordEdit.getText().toString();
        //存儲用戶名和密碼
        editor.putString("account", account);
        editor.putString("password", password);
        //提交
        editor.commit();
        Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();
      }
    });
    //獲取恢復(fù)按鈕對象
    restoreButton = (Button) findViewById(R.id.login2);
    //添加事件
    restoreButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
        //獲取SharedPreference對象
        pref = getSharedPreferences("second", MODE_PRIVATE);
        //讀取內(nèi)容
        String account = pref.getString("account", "this is default value");
        String password = pref.getString("password", "this is default value");
        //設(shè)置到響應(yīng)位置
        EditText editText2 = (EditText)findViewById(R.id.account2);
        editText2.setText(account);
        EditText passwordText2 = (EditText) findViewById(R.id.password2);
        passwordText2.setText(password);
        Toast.makeText(getApplicationContext(), "恢復(fù)成功", Toast.LENGTH_SHORT).show();
      }
    });
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
}

3、結(jié)果

輸入內(nèi)容后,當點擊“save data”按鈕后,存儲文件為second.xml,如下:

對應(yīng)內(nèi)容:

下面是效果圖:

 

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

  • 如何安裝系統(tǒng)認證簽名過的APK

    如何安裝系統(tǒng)認證簽名過的APK

    如果你的App因為權(quán)限原因需要設(shè)置 android:sharedUserId="android.uid.system" 那么IDE編譯出的包通常是無法直接安裝的,查看控制臺會發(fā)現(xiàn)報 INSTALL_FAILED_SHARED_USER_INCOMPATIBLE錯誤。這是必須的,隨隨便便一個App聲明一下就可以和系統(tǒng)用戶共享ID,豈不亂套了?
    2014-07-07
  • Android 無預(yù)覽拍照功能

    Android 無預(yù)覽拍照功能

    最近小編接到一個項目,遇到這樣的需求,要求在后臺拍照并保存功能,也就是無預(yù)覽拍照功能,下面小編給大家?guī)砹藢嵗a,需要的朋友參考下
    2018-02-02
  • Kotlin使用滾動控件RecyclerView實例教程

    Kotlin使用滾動控件RecyclerView實例教程

    RecyclerView是Android一個更強大的控件,其不僅可以實現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實現(xiàn)數(shù)據(jù)縱向滾動,也可以實現(xiàn)橫向滾動(ListView做不到橫向滾動)。接下來講解RecyclerView的用法
    2022-12-12
  • Android中如何取消listview的點擊效果

    Android中如何取消listview的點擊效果

    這篇文章主要介紹了 Android中取消listview的點擊效果的實現(xiàn)方法,通過引用transparent之后會讓點擊效果透明化,一起通過本文學習吧
    2017-01-01
  • Android設(shè)備獲取掃碼槍掃描內(nèi)容

    Android設(shè)備獲取掃碼槍掃描內(nèi)容

    這篇文章主要為大家詳細介紹了Android設(shè)備獲取掃碼槍掃描內(nèi)容,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 詳細介紹Android中回調(diào)函數(shù)機制

    詳細介紹Android中回調(diào)函數(shù)機制

    這篇文章主要介紹了Android中回調(diào)函數(shù)機制,有需要的朋友可以參考一下
    2014-01-01
  • Android編程之ListView和EditText發(fā)布帖子隱藏軟鍵盤功能詳解

    Android編程之ListView和EditText發(fā)布帖子隱藏軟鍵盤功能詳解

    這篇文章主要介紹了Android編程之ListView和EditText發(fā)布帖子隱藏軟鍵盤功能,結(jié)合實例形式分析了Android控件調(diào)用、隱藏軟鍵盤的原理與具體實現(xiàn)技巧,需要的朋友可以參考下
    2017-08-08
  • Flutter中灰屏問題的原因及解決方法

    Flutter中灰屏問題的原因及解決方法

    生產(chǎn)中的 flutter 應(yīng)用程序中的灰屏是一種通用占位符,當框架遇到問題無法渲染預(yù)期用戶界面時就會顯示,所以基本上是出現(xiàn)問題時的后備指示器,本文給大家介紹了Flutter中灰屏問題的原因及解決方法,需要的朋友可以參考下
    2024-06-06
  • Android?數(shù)據(jù)結(jié)構(gòu)全面總結(jié)分析

    Android?數(shù)據(jù)結(jié)構(gòu)全面總結(jié)分析

    這篇文章主要為大家介紹了Android?數(shù)據(jù)結(jié)構(gòu)全面總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • 如何玩轉(zhuǎn)Android矢量圖VectorDrawable

    如何玩轉(zhuǎn)Android矢量圖VectorDrawable

    這篇文章主要教大家如何玩轉(zhuǎn)Android矢量圖VectorDrawable,對矢量圖感興趣的小伙伴們可以參考一下
    2016-05-05

最新評論

江川县| 宽城| 读书| 白玉县| 尤溪县| 晋宁县| 昂仁县| 马尔康县| 米泉市| 霍林郭勒市| 贵定县| 调兵山市| 永康市| 临江市| 涟水县| 昔阳县| 自贡市| 通城县| 科技| 呈贡县| 广丰县| 贵南县| 比如县| 成都市| 股票| 郁南县| 北碚区| 弥渡县| 武乡县| 长丰县| 南岸区| 漠河县| 秦皇岛市| 疏勒县| 通江县| 红桥区| 青龙| 临湘市| 北宁市| 收藏| 鹿邑县|