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

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類中的getSharePreferences()方法

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

2.Activity類中的getPreference()方法

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

3.PreferenceManager類中的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)文章

  • Flutter?Animation實(shí)現(xiàn)縮放和滑動(dòng)動(dòng)畫(huà)效果

    Flutter?Animation實(shí)現(xiàn)縮放和滑動(dòng)動(dòng)畫(huà)效果

    這篇文章主要為大家詳細(xì)介紹了Flutter?Animation實(shí)現(xiàn)縮放和滑動(dòng)動(dòng)畫(huà)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Android實(shí)現(xiàn)多點(diǎn)觸摸操作

    Android實(shí)現(xiàn)多點(diǎn)觸摸操作

    這篇文章主要介紹了Android實(shí)現(xiàn)多點(diǎn)觸摸操作,實(shí)現(xiàn)圖片的放大、縮小和旋轉(zhuǎn)等處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android使用WebView.loadUri()打開(kāi)網(wǎng)頁(yè)的方法

    Android使用WebView.loadUri()打開(kāi)網(wǎng)頁(yè)的方法

    這篇文章主要介紹了Android使用WebView.loadUri()打開(kāi)網(wǎng)頁(yè)的方法,結(jié)合實(shí)例形式分析了Android中WebView控件的loadUri()打開(kāi)網(wǎng)頁(yè)的使用技巧,需要的朋友可以參考下
    2016-01-01
  • Android中Notification 提示對(duì)話框

    Android中Notification 提示對(duì)話框

    Notification,俗稱通知,是一種具有全局效果的通知,它展示在屏幕的頂端,首先會(huì)表現(xiàn)為一個(gè)圖標(biāo)的形式,當(dāng)用戶向下滑動(dòng)的時(shí)候,展示出通知具體的內(nèi)容
    2016-01-01
  • Android自定義控件之自定義屬性(二)

    Android自定義控件之自定義屬性(二)

    這篇文章主要為大家詳細(xì)介紹了Android自定義控件之自定義屬性,如何給自定義控件自定義一些屬性,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android自定義條形對(duì)比統(tǒng)計(jì)圖

    Android自定義條形對(duì)比統(tǒng)計(jì)圖

    這篇文章主要為大家詳細(xì)介紹了Android自定義條形對(duì)比統(tǒng)計(jì)圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • Android 通過(guò)SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)管理

    Android 通過(guò)SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)管理

    SQLiteOpenHelper 是Android 提供的一個(gè)抽象工具類,負(fù)責(zé)管理數(shù)據(jù)庫(kù)的創(chuàng)建、升級(jí)工作。本文主要介紹了如何使用SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)對(duì)數(shù)據(jù)進(jìn)行存儲(chǔ)管理,感興趣的可以了解一下
    2021-11-11
  • Android4.4新增函數(shù)訪問(wèn)外部存儲(chǔ)

    Android4.4新增函數(shù)訪問(wèn)外部存儲(chǔ)

    這篇文章主要介紹了Android4.4新增函數(shù)訪問(wèn)外部存儲(chǔ)的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-10-10
  • Android中button按鈕怎么設(shè)置圓角

    Android中button按鈕怎么設(shè)置圓角

    在android開(kāi)發(fā)中,Button是使用很頻繁的一種控件,而android提供的原生Button是很規(guī)矩的矩形外觀,有時(shí)候缺乏美感,而相反,圓角按鈕則可以提升美感,這篇文章主要給大家介紹了關(guān)于Android中button按鈕怎么設(shè)置圓角的相關(guān)資料,需要的朋友可以參考下
    2023-07-07
  • 你值得擁有的Android Studio開(kāi)發(fā)小技巧

    你值得擁有的Android Studio開(kāi)發(fā)小技巧

    這篇文章主要為大家分享了值得擁有的Android Studio開(kāi)發(fā)小技巧,介紹幾個(gè)比較好用的技巧和快捷鍵,提升我們的編碼效率,感興趣的小伙伴們可以參考一下
    2016-06-06

最新評(píng)論

宣汉县| 惠州市| 永嘉县| 乾安县| 洛隆县| 子洲县| 台州市| 沅江市| 大足县| 融水| 德昌县| 托克逊县| 大连市| 宁阳县| 体育| 巫山县| 桑日县| 兰州市| 定日县| 汉中市| 安顺市| 德庆县| 利川市| 潼关县| 利辛县| 天长市| 苏尼特左旗| 邵东县| 蓬莱市| 黎城县| 聂拉木县| 八宿县| 兰考县| 鹤峰县| 清苑县| 甘泉县| 台前县| 西城区| 合川市| 长子县| 乐至县|