android中SharedPreferences的用法
来源:互联网 发布:排序算法 比较 编辑:程序博客网 时间:2024/06/05 03:48
SharedPreferences的用法非常简单,因为原理和Map差不多,所以下面给出一个参考程序:
MainActivity.java
import android.content.Context;import android.content.SharedPreferences;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashSet;import java.util.Iterator;import java.util.Set;public class MainActivity extends AppCompatActivity { SharedPreferences preferences; SharedPreferences.Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取只能被本应用程序读、写的SharedPreferences对象 preferences = getSharedPreferences("raid", Context.MODE_WORLD_READABLE); editor = preferences.edit(); Button write = (Button)findViewById(R.id.write); Button read = (Button)findViewById(R.id.read); final TextView txt = (TextView)findViewById(R.id.txt); write.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editor.putBoolean("booleanValue", true); editor.putFloat("floatValue", 12.5f); editor.putInt("intValue", 12); editor.putLong("longValue", 22222222); editor.putString("stringValue", getDateString()); Set<String> set = new HashSet<String>(); for(int i = 0; i < 3; i++) { set.add("set"+i); } editor.putStringSet("setValue", set); editor.commit(); } }); read.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { StringBuilder sb = new StringBuilder(); sb.append(preferences.getBoolean("booleanValue", false) + "\n"); sb.append(preferences.getFloat("floatValue", 0.0f) + "\n"); sb.append(preferences.getInt("intValue", 0) + "\n"); sb.append(preferences.getLong("longValue", 0) + "\n"); sb.append(preferences.getString("stringValue", null) + "\n"); sb.append("the set is : " + "\n"); Set<String> set = preferences.getStringSet("setValue", null); if(set != null) { Iterator<String> iterator = set.iterator(); while(iterator.hasNext()) { sb.append(iterator.next() + "\n"); } } txt.setText(sb.toString()); } }); } private String getDateString() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日hh:mm:ss"); return sdf.format(new Date()); }}
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/write" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/write" android:paddingRight="20dp"/> <Button android:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/read" /> </LinearLayout> <TextView android:id="@+id/txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1"/></LinearLayout>
0 0
- android中SharedPreferences的用法
- Android中sharedPreferences的用法
- android中sharedPreferences的用法
- android中sharedPreferences的用法
- android中sharedPreferences的用法
- android中sharedPreferences的用法
- android中sharedPreferences的用法
- android中sharedPreferences的用法
- Android中SharedPreferences的用法
- android中sharedPreferences的用法
- android中SharedPreferences的用法
- android中sharedPreferences的用法
- android中sharedPreferences的用法
- android中sharedPreferences的用法
- android中sharedPreferences的用法
- android中SharedPreferences的用法
- android中sharedPreferences的用法
- android中sharedPreferences的用法
- 华为上机笔试题
- 项目制图的简单准备
- 关于C++表达式求值的困惑
- 容斥定理
- 数据结构(用面象对象方法与C++语言描述)线性表
- android中SharedPreferences的用法
- react-native试玩(14)-iOS中进度栏
- linux文件系统
- Python 读取csv的某行
- 一起来学android(3)
- Python学习----面向对象编程
- POJ 3045 Cow Acrobats(贪心 or 二分)
- JBoss 系列九十一:JBoss 构建的高可用集群环境使用 Apache 的 mod_rewrite 转发根路径到特定应用
- uva 11865 - Stream My Contest(二分+有向最小生成树)