关于偏好设置文件的使用

关于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;
                }
            });
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值