0
点赞
收藏
分享

微信扫一扫

android中SharedPreferences的用法


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 条评论