`

Android 中Preferences的使用!

阅读更多
大家好,我们这一节讲的是Android Preferences 的学习,Preferences 在Android当中被用来记录应用,以及用户喜好等等,它可以用来保存

简单的数据类型,如Int,Double,Boolean等。Preferences中保存的数据可以理解为Map型。我们通过PreferenceManager 以及getDefaultSharedPreferences(Context) 来获取它,比如当我们想获得整数我们可以用 getInt(String key, int defVal) .获取里面的某个键值,当我们想修改时候我们用 putInt(String key, int newVal), 最后用 edit(), 方法提交!千万不要忘记了哦~



为了让大家跟好的理解我做了一个简单的Demo,程序主要有个TextView控件,上面写着用户使用该应用的次数。

下面是实现Demo的大体步骤:



一、新建一个Android工程命名为:PreferencesDemo。



二、在修改main.xml布局文件,这里只是在TextView控件里加了一个id.代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:id="@+id/text"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>


三、修改PreferenceDemo.java的代码,全部代码如下:
package com.android.tutor;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TextView;
public class PreferencesDemo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        SharedPreferences mPerferences = PreferenceManager
        .getDefaultSharedPreferences(this);
        
        int counter = mPerferences.getInt("counter", 0);
        
        TextView mTextView = (TextView)findViewById(R.id.text);
        
        mTextView.setText("This app has been started " + counter + " times.");
        
        SharedPreferences.Editor mEditor = mPerferences.edit();
        
        mEditor.putInt("counter", ++counter);
        mEditor.commit();
        
    }
}


查看Preferences文件,首先打开命令终端:adb shell一下,然后cd data/data进入该目录,ls一下我们会发现一大堆包文件
cd com.android.tutor (这里是我程序的包名) /shared_prefs,ls一下会发现.xml文件如下图
打开.xml文件,格式如下(为什么这样大家自己去理解):
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<int name="counter" value="3" />
</map>
分享到:
评论

相关推荐

    Android高手进阶教程与Android基础教程

    Android高手进阶教程之----Android 中Preferences的使用! .doc Android高手进阶教程之----Android 中自定义View的应用.doc Android高手进阶教程之----Android 中自定义属性(attr.xml,TypedArray)的使用! .doc ...

    Android代码-android-secure-preferences

    android-secure-preferences About This project uses the Encryption class from: http://www.java2s.com/Code/Android/Security/AESEncryption.htm Gives an implementation of SharedPreferences, which encrypts...

    Android 使用Shared Preferences进行数据存储-样例.rar

    Android 使用Shared Preferences进行数据存储-样例,演示如何使用 Shared Preferences 获得数据和保存数据、如何使用getPreferences方法创建文件的模式,以及如何使用getPreferences模拟用户参数设置、查看 ...

    Android代码-一个快速方便地访问Android Shared preferences 库。

    FastSave is An Android library for fast and easy access to Android Shared preferences. It allows you to save any type or list in the sharedpreferences and retrieve it in convenient way. Installation ...

    Android代码-Multiplatform-Preferences

    Compatible with kotlin android and kotlin native for iphone class MyPresenter { val preferences = Preferences() fun start(){ preferences.getString("userName")?.let { view.displayUser(it) } val...

    android 数据存取Preferences

    他的用法基本上和J2SE(java.util.prefs.Preferences)中的用法一样,以一种简单、 透明的方式来保存一些用户个性化设置的字体、颜色、位置等参数信息。一般的应用程序都会提供“设置”或者“首选项”的这样的界面,...

    Android Preferences

    Tutorial Android Preferences

    android 定制preferences布局和自定义对话框(左边带图标的preferences)

    很想做个天气预警的功能, 想用preferences来做界面。 看了很多preferences感觉定制性太差 所以自己做了一个。 应该是不错的demo 定制preferences在preferencesActivity中的布局 和自定义了对话框的布局

    Android代码-rx-preferences

    Reactive SharedPreferences for Android. Usage Create an RxSharedPreferences instance which wraps a SharedPreferences: SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences...

    android-prefs:.zip

    android-prefs:.zip,Android preferences for WINNERS!

    Preferences_android_

    Use of Preferences in Android pdf

    Android代码-preferences-helper

    preferences-helper SharePreferences is very popular with any project and all most all project has SharePreferences for saving data. This library will help you faster in configuration and use ...

    Preferences

    android设置一些常用的属性,如ip地址和端口号什么,都是可以用android的Preferences来设置,简单方便。

    android-preferences,.zip

    此回购协议已迁移到github.com/android/user-interface。请检查回购协议以了解未来的更新。谢谢您!

    Android代码-KotlinPreferences

    Kotlin Android Library, that makes preference usage simple and fun. KotlinPreferences now have a brother. With KotlinPreferences, you can define different preference fields this way: var ...

    Android代码-shared-preferences-helper

    Android Shared Preferences Helper Android Library to handle SharedPreferences boilerplate code and other tools Download dependencies { compile 'com.github.seanzor:shared-preferences-helper:1.1.0' }...

    Android数据持久化之Preferences机制详解

    在Android中,实现数据持久化有五种方式:Preferences,文件File,I/O操作、SQLite数据库,ContentProvider组件。 下面逐个做一简单的介绍: 一、Preferences的介绍: Preferences是一种轻量级的数据存储机制,他将...

    Android Preferences保存数据的简单实例

    NULL 博文链接:https://liyf155.iteye.com/blog/1455717

    关于android的数据存储-SQLite-ContentProvider-preferences

    关于android的数据存储-SQLite-ContentProvider-preferences

    Android preferences演示源代码

    Preferences主要用于存储和查询简单数据类型的数据,这些简单数据类型包括boolean、int、float、long以及 String 等,存储方式以键值对的形式存放在应用程序私有的文件夹下。    代码片段:   public void ...

Global site tag (gtag.js) - Google Analytics