欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android學(xué)習(xí)之SharedPerference存儲(chǔ)詳解

 更新時(shí)間:2017年08月09日 10:02:58   作者:天秤心已隨風(fēng)去  
這篇文章主要為大家詳細(xì)介紹了Android學(xué)習(xí)之SharedPerference存儲(chǔ)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

SharedPerference不同同于文件存儲(chǔ),它是使用鍵值的方式來(lái)存儲(chǔ)數(shù)據(jù),對(duì)于保存的每一條數(shù)據(jù)都會(huì)給一個(gè)鍵值,這樣在讀取數(shù)據(jù)時(shí)直接通過(guò)鍵值取出相應(yīng)數(shù)據(jù)。amdroid提供了三個(gè)方法來(lái)獲取實(shí)例:

1.Context類(lèi)中的getSharePreferences()方法

它接收兩個(gè)參數(shù),第一個(gè)是文件名;第二個(gè)是操作模式,目前只有MODE_PRIVATE可選,這是默認(rèn)的操作模式,表示只有當(dāng)前的應(yīng)用可以對(duì)文件進(jìn)行操作。

2.Activity類(lèi)中的getPreference()方法

它只接收一個(gè)操作模式參數(shù),因?yàn)槭褂眠@個(gè)方法會(huì)自動(dòng)將類(lèi)名SharedPreference作為文件名。

3.PreferenceManager類(lèi)中的getDefaultSharedPreference()方法

主要由三步來(lái)實(shí)現(xiàn):

  (1)調(diào)用SharedPreferences對(duì)象的edit()方法來(lái)獲取一個(gè)SharedPreferences.Editor對(duì)象。
  (2)向SharedPreferences.Editor對(duì)象中添加數(shù)據(jù),比如添加一個(gè)布爾型數(shù)據(jù)就使用putBoolean()方法,依次論推。
  (3)調(diào)用apply()方法將添加的數(shù)據(jù)提交,從而完成數(shù)據(jù)操作。`

使用案例

MainActivity:

package com.example.sharedpreferences;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

  private Button button;
  private Button restore_btn;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button)findViewById(R.id.save_data);
    button.setOnClickListener(this);
    restore_btn = (Button)findViewById(R.id.restore_data);
    restore_btn.setOnClickListener(this);
  }

  @Override
  public void onClick(View view) {
    switch (view.getId()){
      case R.id.save_data:
        saveData();
        Toast.makeText(MainActivity.this,"保存成功!",Toast.LENGTH_SHORT).show();
        break;
      case R.id.restore_data:
        restorData();
        Toast.makeText(MainActivity.this,"恢復(fù)成功",Toast.LENGTH_SHORT).show();
        break;
      default:
        break;
    }
  }
  public void saveData(){
    SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
    editor.putString("name","Tom");
    editor.putInt("age",18);
    editor.putBoolean("married",false);
    editor.apply();
  }
  public void restorData(){
    SharedPreferences preferences = getSharedPreferences("data",MODE_PRIVATE);
    String name = preferences.getString("name","");
    int age = preferences.getInt("age",0);
    boolean married = preferences.getBoolean("marred",false);
    Log.d("主活動(dòng): ","獲取到名字: "+name);
    Log.d("主活動(dòng): ","獲取到年齡: "+age);
    Log.d("主活動(dòng): ","獲取到婚配: "+married);
  }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.sharedpreferences.MainActivity">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <Button
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/save_data"
      android:text="保存數(shù)據(jù)"/>
    <Button
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/restore_data"
      android:text="恢復(fù)數(shù)據(jù)"/>
  </LinearLayout>

</android.support.constraint.ConstraintLayout>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android中webview與JS交互、互調(diào)方法實(shí)例詳解

    Android中webview與JS交互、互調(diào)方法實(shí)例詳解

    這篇文章主要介紹了Android中webview與JS交互、互調(diào)方法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Android 個(gè)人理財(cái)工具四:添加賬單頁(yè)面 下

    Android 個(gè)人理財(cái)工具四:添加賬單頁(yè)面 下

    本文主要介紹Android 個(gè)人理財(cái)工具添加賬單頁(yè)面,這里是添加賬單的詳情頁(yè)面及如何使用Android Spinner控件的簡(jiǎn)單示例,有需要的小伙伴可以參考下
    2016-08-08
  • Kotlin擴(kuò)展函數(shù)超詳細(xì)介紹

    Kotlin擴(kuò)展函數(shù)超詳細(xì)介紹

    Kotlin?可以為一個(gè)不能修改的或來(lái)自第三方庫(kù)中的類(lèi)編寫(xiě)一個(gè)新的函數(shù)。?這個(gè)新增的函數(shù)就像那個(gè)原始類(lèi)本來(lái)就有的函數(shù)一樣,可以用普通的方法調(diào)用,這種機(jī)制的函數(shù)稱(chēng)為擴(kuò)展函數(shù)
    2022-09-09
  • Android自定義View模仿虎撲直播界面的打賞按鈕功能

    Android自定義View模仿虎撲直播界面的打賞按鈕功能

    這篇文章主要介紹了Android自定義View模仿虎撲直播界面的打賞按鈕功能,文中介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-04-04
  • Android8.1 源碼修改之插入SIM卡默認(rèn)啟用Volte功能

    Android8.1 源碼修改之插入SIM卡默認(rèn)啟用Volte功能

    這篇文章主要介紹了Android8.1 源碼修改之插入SIM卡默認(rèn)啟用Volte功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05
  • Android?Jetpack庫(kù)剖析之LiveData組件篇

    Android?Jetpack庫(kù)剖析之LiveData組件篇

    LiveData是Jetpack組件的一部分,更多的時(shí)候是搭配ViewModel來(lái)使用,相對(duì)于Observable,LiveData的最大優(yōu)勢(shì)是其具有生命感知的,換句話(huà)說(shuō),LiveData可以保證只有在組件( Activity、Fragment、Service)處于活動(dòng)生命周期狀態(tài)的時(shí)候才會(huì)更新數(shù)據(jù)
    2022-07-07
  • Android藍(lán)牙服務(wù)查找附近設(shè)備分析探索

    Android藍(lán)牙服務(wù)查找附近設(shè)備分析探索

    這篇文章主要介紹了Android藍(lán)牙服務(wù)實(shí)現(xiàn)查找附近設(shè)備,了解內(nèi)部原理是為了幫助我們做擴(kuò)展,同時(shí)也是驗(yàn)證了一個(gè)人的學(xué)習(xí)能力,如果你想讓自己的職業(yè)道路更上一層樓,這些底層的東西你是必須要會(huì)的
    2023-01-01
  • Android自定義View仿微信LetterView效果

    Android自定義View仿微信LetterView效果

    這篇文章主要介紹了Android自定義View仿微信LetterView效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒借鑒價(jià)值,需要的朋友可以參考下
    2017-03-03
  • Android開(kāi)發(fā)實(shí)現(xiàn)讀取excel數(shù)據(jù)并保存為xml的方法

    Android開(kāi)發(fā)實(shí)現(xiàn)讀取excel數(shù)據(jù)并保存為xml的方法

    這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)讀取excel數(shù)據(jù)并保存為xml的方法,涉及Android針對(duì)Excel數(shù)據(jù)讀取及xml格式文件的構(gòu)造與保存相關(guān)操作技巧,需要的朋友可以參考下
    2017-10-10
  • Android頁(yè)面中引導(dǎo)蒙層的使用方法詳解

    Android頁(yè)面中引導(dǎo)蒙層的使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Android頁(yè)面中的引導(dǎo)蒙層使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03

最新評(píng)論