热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SharedPreferences类的使用

SharedPreferences,用xml文件保存用户的偏好设置,是一个轻量级的存储类。demo效果: 代码:activity_main

SharedPreferences,用xml文件保存用户的偏好设置,是一个轻量级的存储类。

demo效果:

 

代码:

activity_main


xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:gravity="center"
tools:cOntext=".MainActivity">

android:id="@+id/textview"
android:layout_
android:layout_
android:text=
"Hello World!" />

 

 

activity_settings



xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:name="com.example.admin.sharedpreferences.SettingsActivity$preferenceFragment"
android:id="@+id/preference"/>



menu.menu

 


xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
android:id="@+id/menu_settings"
android:title="settings"
app:showAsAction="never"/>

 

 

arrays




black
red
blue
green







xmlns:android="http://schemas.android.com/apk/res/android">

android:title="change word"
android:defaultValue="false"
android:summaryOn="Show"
android:summaryOff="Hidden"
android:key="word"/>

android:title="size"
android:defaultValue="15"
android:inputType="numberDecimal"
android:key="size"/>

android:title="color"
android:defaultValue="black"
android:entries="@array/color_show"
android:entryValues="@array/color_show"
android:key="color"/>





package="com.example.admin.sharedpreferences">

android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">








android:name=".SettingsActivity"
android:parentActivityName=".MainActivity">
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>




MainActivity.java

package com.example.admin.sharedpreferences;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
switch (id){
case R.id.menu_settings:
Intent intent=new Intent(this,SettingsActivity.class);
startActivity(intent);
break;
}
return true;
}


@Override
protected void onStart() {
super.onStart();

SharedPreferences sharedPreferences=PreferenceManager.getDefaultSharedPreferences(this);
TextView textView=findViewById(R.id.textview);
if (sharedPreferences.getBoolean("word",false)){
textView.setText("bye!");
}
String text=sharedPreferences.getString("color","black");
if (text.equals("black")){
textView.setTextColor(Color.BLACK);
}else if (text.equals("red")){
textView.setTextColor(Color.RED);
}else if (text.equals("blue")){
textView.setTextColor(Color.BLUE);
}else if (text.equals("green")){
textView.setTextColor(Color.GREEN);
}

String sizeText=sharedPreferences.getString("size","15");
int size=Integer.parseInt(sizeText);
textView.setTextSize(size);
}


SettingsActivity.java

package com.example.admin.sharedpreferences;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;


public class SettingsActivity extends AppCompatActivity {
@Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
}


@Override
public void onBackPressed() {
super.onBackPressed();
NavUtils.navigateUpFromSameTask(this);
}

public static class preferenceFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener {

@Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences_settings);
Preference preference_size = findPreference("size");
preferenceSummary(preference_size);
Preference preference_color = findPreference("color");
preferenceSummary(preference_color);
}

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String value=String.valueOf(newValue);
if (preference instanceof ListPreference){
ListPreference listPreference=(ListPreference) preference;
int index=listPreference.findIndexOfValue(value);
if (index >=0){
CharSequence[] text=listPreference.getEntries();
preference.setSummary(text[index]);
}
}else {
preference.setSummary(value);
}
return true;
}

public void preferenceSummary(Preference preference) {
preference.setOnPreferenceChangeListener(this );
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(preference.getContext());
String text = sharedPreferences.getString(preference.getKey(), "");
onPreferenceChange(preference, text);
}
}

}



推荐阅读
author-avatar
拍友2502914573
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有