国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

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

2019-10-22 18:30:24
字體:
供稿:網(wǎng)友

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

1.Context類中的getSharePreferences()方法

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

2.Activity類中的getPreference()方法

它只接收一個操作模式參數(shù),因為使用這個方法會自動將類名SharedPreference作為文件名。

3.PreferenceManager類中的getDefaultSharedPreference()方法

主要由三步來實現(xiàn):

  (1)調(diào)用SharedPreferences對象的edit()方法來獲取一個SharedPreferences.Editor對象。
  (2)向SharedPreferences.Editor對象中添加數(shù)據(jù),比如添加一個布爾型數(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("主活動: ","獲取到名字: "+name);    Log.d("主活動: ","獲取到年齡: "+age);    Log.d("主活動: ","獲取到婚配: "+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>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 颍上县| 莲花县| 冀州市| 芜湖县| 柳江县| 宝清县| 东明县| 仲巴县| 贡嘎县| 延川县| 内黄县| 广水市| 宁海县| 弥勒县| 上栗县| 大姚县| 台安县| 墨竹工卡县| 普兰店市| 葫芦岛市| 巴塘县| 汶上县| 岳池县| 宣威市| 高雄市| 融水| 北辰区| 丰城市| 白山市| 吉隆县| 衡东县| 山阴县| 罗田县| 博兴县| 正宁县| 滨海县| 汶川县| 温宿县| 天气| 潼南县| 贵港市|