关于Android偏好设置文件的使用
页面布局:
<RelativeLayout 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: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" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="97dp"
android:onClick="playNotify"
android:text="播放" />
<CheckBox
android:id="@+id/cb"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="129dp"
android:text="允许声音" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/cb"
android:layout_alignLeft="@+id/cb"
android:layout_marginBottom="60dp"
android:text="使用字体" />
</RelativeLayout>
SharedPreferencesUtil.java:
package com.example.spdemo;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
public class SharedPreferencesUtil {
private static Editor editor;
private SharedPreferences sp;
public SharedPreferencesUtil(Context context){
sp = PreferenceManager.getDefaultSharedPreferences(context);
editor = sp.edit();
}
public SharedPreferencesUtil(Context context,String fileName){
sp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
editor = sp.edit();
}
public boolean isAllowSound(){
return sp.getBoolean("sound", true);
}
public boolean isAllowFont(){
return sp.getBoolean("font", false);
}
public void setAllowSound(boolean flag){
editor.putBoolean("sound", flag);
editor.commit();
}
public void setAllowFont(boolean flag){
editor.putBoolean("font", flag);
editor.commit();
}
}
MainActivity :
package com.example.spdemo;
import android.app.Activity;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MainActivity extends Activity {
CheckBox cb;
CheckBox cb2;
MediaPlayer mplayer;
SharedPreferencesUtil spu;
boolean isPlaying;
Typeface tf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb = (CheckBox) findViewById(R.id.cb);
spu = new SharedPreferencesUtil(this);
if (spu.isAllowSound()) {
cb.setChecked(true);
} else {
cb.setChecked(false);
}
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
spu.setAllowSound(isChecked);
}
});
cb2 = (CheckBox) findViewById(R.id.checkBox1);
if (spu.isAllowFont()) {
cb2.setChecked(true);
// 字体文件 assets/fonts/STXINGKA.TTF
tf = Typeface.createFromAsset(getAssets(), "fonts/STXINGKA.TTF");
cb.setTypeface(tf);
cb2.setTypeface(tf);
} else {
cb2.setChecked(false);
cb.setTypeface(null);
cb2.setTypeface(null);
}
cb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
spu.setAllowFont(isChecked);
if (isChecked) {
cb.setTypeface(tf);
cb2.setTypeface(tf);
} else {
cb.setTypeface(null);
cb2.setTypeface(null);
}
}
});
}
public void playNotify(View v) {
if (isPlaying) {
return;
}
if (!cb.isChecked()) {
return;
}
if (mplayer == null) {
// 音频文件 res/raw/notify.wav
mplayer = MediaPlayer.create(this, R.raw.notify);
mplayer.start();
isPlaying = true;
mplayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
isPlaying = false;
mplayer.release();
mplayer = null;
}
});
}
}
}