SharedPreferences类的使用

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

效果图:

 

代码:

activity_main

<?xml version="1.0" encoding="utf-8"?>
<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:gravity="center"
tools:context=".MainActivity">

<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Hello_World" />


</LinearLayout>

 

 

activity_settings

<?xml version="1.0" encoding="utf-8"?>

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


menu.menu
 
 
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_settings"
android:title="@string/menu_title"
app:showAsAction="never"/>
</menu>
 

 

 

arrays

 
 
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="color_show">
<item>@string/black</item>
<item>@string/red</item>
<item>@string/blue</item>
<item>@string/green</item>
</string-array>
</resources>


preferences_settings

 
 
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">

<CheckBoxPreference
android:title="@string/CheckBoxPreference_title"
android:defaultValue="false"
android:summaryOn="@string/CheckBoxPreference_summaryOn"
android:summaryOff="@string/CheckBceoxPreferen_summaryOff"
android:key="@string/CheckBceoxPreferen_key"/>

<EditTextPreference
android:title="@string/EditTextPreference_title"
android:defaultValue="@string/EditTextPreference_defaultValue"
android:inputType="numberDecimal"
android:selectAllOnFocus="true"
android:key="@string/EditTextPreference_key"/>

<ListPreference
android:title="@string/ListPreference_title"
android:defaultValue="@string/ListPreference_defaultValue"
android:entries="@array/color_show"
android:entryValues="@array/color_show"
android:key="@string/ListPreference_key"/>
</PreferenceScreen>
 
AndroidMainFest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.sharedpreferences">

<application
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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".SettingsActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
</application>

</manifest>

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 {

private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textview);
}

@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);

if (sharedPreferences.getBoolean(getString(R.string.CheckBceoxPreferen_key), false)) {
textView.setText(R.string.Bye);
}

String colorText = sharedPreferences.getString(getString(R.string.ListPreference_key), getString(R.string.ListPreference_defaultValue));
String[] color_array = getResources().getStringArray(R.array.color_show);

for (int i = 0; i < color_array.length; i++) {

if (color_array[i] .equals(colorText)){
setTextColor(i);
break;
}
}

String sizeText = sharedPreferences.getString(getString(R.string.EditTextPreference_key), getString(R.string.EditTextPreference_defaultValue));
int size = Integer.parseInt(sizeText);
textView.setTextSize(size);
}


public void setTextColor(int number) {
switch (number) {
default:
textView.setTextColor(Color.BLACK);
case 0:
textView.setTextColor(Color.BLACK);
break;
case 1:
textView.setTextColor(Color.RED);
break;
case 2:
textView.setTextColor(Color.BLUE);
break;
case 3:
textView.setTextColor(Color.GREEN);
break;

}
}
}

 


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(getString(R.string.EditTextPreference_key));
preferenceSummary(preference_size);
Preference preference_color = findPreference(getString(R.string.ListPreference_key));
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);
}
}

}
 
github项目源码: https://github.com/NeoWu55/Android-Sharedpreferences

转载于:https://www.cnblogs.com/neowu/p/10644742.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值