sharepreferences
2016-05-19 17:41
337 查看
1,sharepreferences在程序中创建一个xml
SharedPreferences myshareperferemces=getSharedPreferences("yuhan", Activity.MODE_PRIVATE);
//创建一个yuhan的xml,如果存在则直接引用,存在data->data->projactname->share_prefs中xml,模式加密本程序才能访问
SharedPreferences.Editor editor=myshareperferemces.edit();
//开始编辑xml
//put里边的值如果不存在,则创建,并且给一个默认值
editor.putBoolean("issuccessed", true);
editor.putFloat("float", (float) 1.5);
editor.putInt("int", 1);
editor.putLong("long", 1);
editor.putString("string", "string");
editor.apply();
//异步线程更新
boolean isyes=myshareperferemces.getBoolean("issuccessed", false);
int number=myshareperferemces.getInt("int", 0);
//从yuhan.xml中拿到对应的值,如果不存在,则得到默认值
Map<String,?> allPreferences=myshareperferemces.getAll();
Boolean containlastfloat=allPreferences.containsKey("lastfloat");
产生的yuhan.xml
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<map>
<boolean value="true" name="issuccessed"/>
<long value="1" name="long"/>
<float value="1.5" name="float"/>
<int value="1" name="int"/>
<string name="string">string</string>
</map>
2,
package com.example.hello;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.appcompat.R.dimen;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;
public class MainActivity extends Activity {
CheckBox autoUpdate;//是否自动更新
Spinner updateFreSpinner;//更新频率
Spinner magnitudeSpinner;//更新时间间隔
SharedPreferences prefs;//得到sharepreferences
public static final String USER_PREFERENCE="USER_PREFERENCE";
public static final String PREF_AUTO_UPDATE="PREF_AUTO_UPDATE";
public static final String PREF_MIN_MAG_INDEX="PREF_MIN_MAG_INDEX";
public static final String PREF_UPDATE_FREQ_INDEX="PREF_UPDATE_FREQ_INDEX";
//这种写法将非常常用,应该要养成习惯,代表值应该要写在程序的上方,或者单独要一个文件夹来写
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preferences);
updateFreSpinner=(Spinner) findViewById(R.id.spinner_update_freq);
magnitudeSpinner=(Spinner) findViewById(R.id.spinner_quake_mag);
autoUpdate=(CheckBox) findViewById(R.id.checkbox);
//初始化控件
populateSpinners();//将值放到下拉控件上
Context context=getApplicationContext();//得到当前context
prefs=PreferenceManager.getDefaultSharedPreferences(context);//得到context默认的sharepreferences
updateUIFromPreferences();
//初始化context中的键值对,并把defaultsharepreferences的值给spinner,等待以后选择,更新
Button okButton=(Button) findViewById(R.id.okButton);
//点击ok按钮,defaultsharepreferences就要更新键值对了
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
savePreferences();//点击了ok,键值对的值要更新了
finish();
}
private void savePreferences() {
// TODO Auto-generated method stub
int updateIndex=updateFreSpinner.getSelectedItemPosition();
int minMagIndex=magnitudeSpinner.getSelectedItemPosition();
Boolean autoUpdateChecked=autoUpdate.isChecked();
Editor editor=prefs.edit();
editor.putBoolean(PREF_AUTO_UPDATE, autoUpdateChecked);
editor.putInt(PREF_UPDATE_FREQ_INDEX, updateIndex);
editor.putInt(PREF_MIN_MAG_INDEX, minMagIndex);
editor.commit();
}
});
}
private void updateUIFromPreferences() {
// TODO Auto-generated method stub
Boolean autoUpChecked=prefs.getBoolean(PREF_AUTO_UPDATE, false);
int updateFreqIndex=prefs.getInt(PREF_UPDATE_FREQ_INDEX, 2);
updateFreSpinner.setSelection(updateFreqIndex);
autoUpdate.setChecked(autoUpChecked);
}
private void populateSpinners() {
// TODO Auto-generated method stub
ArrayAdapter<CharSequence> fAdapter;
fAdapter=ArrayAdapter.createFromResource(this, R.array.update_freq_options, android.R.layout.simple_spinner_item);
int spinner_dd_item=android.R.layout.simple_spinner_dropdown_item;
fAdapter.setDropDownViewResource(spinner_dd_item);
updateFreSpinner.setAdapter(fAdapter);
ArrayAdapter<CharSequence> mAdapter;
mAdapter=ArrayAdapter.createFromResource(this, R.array.magnitude_options, android.R.layout.simple_spinner_item);
mAdapter.setDropDownViewResource(spinner_dd_item);
magnitudeSpinner.setAdapter(mAdapter);
}
}
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="update_freq_options">
<item >Every Minute</item>
<item >5 minutes</item>
<item >10 minutes</item>
<item >15 minutes</item>
<item >Every Hour</item>
</string-array>
<string-array name="magnitude">
<item >3</item>
<item >5</item>
<item >6</item>
<item >7</item>
<item >8</item>
</string-array>
<string-array name="magnitude_options">
<item >3</item>
<item >5</item>
<item >6</item>
<item >7</item>
<item >8</item>
</string-array>
<string-array name="update_freq_values">
<item >1</item>
<item >5</item>
<item >10</item>
<item >15</item>
<item >60</item>
</string-array>
</resources>
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">hello</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="menu_update"> Refresh Earthquakes</string>
<string name="auto_update_prompt">Auto Update?</string>
<string name="update_freq_prompt">Update Frequency</string>
<string name="min_quake_mag_prompt">Minimun Quake Magnitude</string>
<string name="menu_preferences">Preferences</string>
</resources>
preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/auto_update_prompt"/>
<CheckBox
android:id="@+id/ckeckbox_auto_update"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/update_freq_prompt"/>
<Spinner
android:id="@+id/spinner_update_freq"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/min_quake_mag_prompt"/>
<Spinner
android:id="@+id/spinner_quake_mag"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/okButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@android:string/ok"/>
<Button
android:id="@+id/cannelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@android:string/cancel"/>
</LinearLayout>
</LinearLayout>
SharedPreferences myshareperferemces=getSharedPreferences("yuhan", Activity.MODE_PRIVATE);
//创建一个yuhan的xml,如果存在则直接引用,存在data->data->projactname->share_prefs中xml,模式加密本程序才能访问
SharedPreferences.Editor editor=myshareperferemces.edit();
//开始编辑xml
//put里边的值如果不存在,则创建,并且给一个默认值
editor.putBoolean("issuccessed", true);
editor.putFloat("float", (float) 1.5);
editor.putInt("int", 1);
editor.putLong("long", 1);
editor.putString("string", "string");
editor.apply();
//异步线程更新
boolean isyes=myshareperferemces.getBoolean("issuccessed", false);
int number=myshareperferemces.getInt("int", 0);
//从yuhan.xml中拿到对应的值,如果不存在,则得到默认值
Map<String,?> allPreferences=myshareperferemces.getAll();
Boolean containlastfloat=allPreferences.containsKey("lastfloat");
产生的yuhan.xml
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<map>
<boolean value="true" name="issuccessed"/>
<long value="1" name="long"/>
<float value="1.5" name="float"/>
<int value="1" name="int"/>
<string name="string">string</string>
</map>
2,
package com.example.hello;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.appcompat.R.dimen;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;
public class MainActivity extends Activity {
CheckBox autoUpdate;//是否自动更新
Spinner updateFreSpinner;//更新频率
Spinner magnitudeSpinner;//更新时间间隔
SharedPreferences prefs;//得到sharepreferences
public static final String USER_PREFERENCE="USER_PREFERENCE";
public static final String PREF_AUTO_UPDATE="PREF_AUTO_UPDATE";
public static final String PREF_MIN_MAG_INDEX="PREF_MIN_MAG_INDEX";
public static final String PREF_UPDATE_FREQ_INDEX="PREF_UPDATE_FREQ_INDEX";
//这种写法将非常常用,应该要养成习惯,代表值应该要写在程序的上方,或者单独要一个文件夹来写
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preferences);
updateFreSpinner=(Spinner) findViewById(R.id.spinner_update_freq);
magnitudeSpinner=(Spinner) findViewById(R.id.spinner_quake_mag);
autoUpdate=(CheckBox) findViewById(R.id.checkbox);
//初始化控件
populateSpinners();//将值放到下拉控件上
Context context=getApplicationContext();//得到当前context
prefs=PreferenceManager.getDefaultSharedPreferences(context);//得到context默认的sharepreferences
updateUIFromPreferences();
//初始化context中的键值对,并把defaultsharepreferences的值给spinner,等待以后选择,更新
Button okButton=(Button) findViewById(R.id.okButton);
//点击ok按钮,defaultsharepreferences就要更新键值对了
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
savePreferences();//点击了ok,键值对的值要更新了
finish();
}
private void savePreferences() {
// TODO Auto-generated method stub
int updateIndex=updateFreSpinner.getSelectedItemPosition();
int minMagIndex=magnitudeSpinner.getSelectedItemPosition();
Boolean autoUpdateChecked=autoUpdate.isChecked();
Editor editor=prefs.edit();
editor.putBoolean(PREF_AUTO_UPDATE, autoUpdateChecked);
editor.putInt(PREF_UPDATE_FREQ_INDEX, updateIndex);
editor.putInt(PREF_MIN_MAG_INDEX, minMagIndex);
editor.commit();
}
});
}
private void updateUIFromPreferences() {
// TODO Auto-generated method stub
Boolean autoUpChecked=prefs.getBoolean(PREF_AUTO_UPDATE, false);
int updateFreqIndex=prefs.getInt(PREF_UPDATE_FREQ_INDEX, 2);
updateFreSpinner.setSelection(updateFreqIndex);
autoUpdate.setChecked(autoUpChecked);
}
private void populateSpinners() {
// TODO Auto-generated method stub
ArrayAdapter<CharSequence> fAdapter;
fAdapter=ArrayAdapter.createFromResource(this, R.array.update_freq_options, android.R.layout.simple_spinner_item);
int spinner_dd_item=android.R.layout.simple_spinner_dropdown_item;
fAdapter.setDropDownViewResource(spinner_dd_item);
updateFreSpinner.setAdapter(fAdapter);
ArrayAdapter<CharSequence> mAdapter;
mAdapter=ArrayAdapter.createFromResource(this, R.array.magnitude_options, android.R.layout.simple_spinner_item);
mAdapter.setDropDownViewResource(spinner_dd_item);
magnitudeSpinner.setAdapter(mAdapter);
}
}
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="update_freq_options">
<item >Every Minute</item>
<item >5 minutes</item>
<item >10 minutes</item>
<item >15 minutes</item>
<item >Every Hour</item>
</string-array>
<string-array name="magnitude">
<item >3</item>
<item >5</item>
<item >6</item>
<item >7</item>
<item >8</item>
</string-array>
<string-array name="magnitude_options">
<item >3</item>
<item >5</item>
<item >6</item>
<item >7</item>
<item >8</item>
</string-array>
<string-array name="update_freq_values">
<item >1</item>
<item >5</item>
<item >10</item>
<item >15</item>
<item >60</item>
</string-array>
</resources>
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">hello</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="menu_update"> Refresh Earthquakes</string>
<string name="auto_update_prompt">Auto Update?</string>
<string name="update_freq_prompt">Update Frequency</string>
<string name="min_quake_mag_prompt">Minimun Quake Magnitude</string>
<string name="menu_preferences">Preferences</string>
</resources>
preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/auto_update_prompt"/>
<CheckBox
android:id="@+id/ckeckbox_auto_update"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/update_freq_prompt"/>
<Spinner
android:id="@+id/spinner_update_freq"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/min_quake_mag_prompt"/>
<Spinner
android:id="@+id/spinner_quake_mag"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/okButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@android:string/ok"/>
<Button
android:id="@+id/cannelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@android:string/cancel"/>
</LinearLayout>
</LinearLayout>
相关文章推荐
- jquery修改css的两种方法
- 如何解决transfer error: Read-only file system
- CSS @font-face属性实现在网页中嵌入任意字体
- nyoj248 BUYING FEED(贪心orDP)
- js 中 foreach循环(for in)中,最后会多出一个值
- 关于js的时间对象常用方法
- jquery的height()和javascript的height总结,js获取屏幕高度
- iframe使用方法之:用js操纵iframe里面的元素
- javascript递归函数的正确写法
- 常用JavaScript 框架比较
- js基础和数据操作
- 你必须知道的Javascript 系列
- bootstrap-datetimepicker初学者经验
- IOS5中的Safari不兼容Javascript中的Date问题,做下笔录吧!
- ReactNative动画(下)
- ReactNative动画(上)
- jquery width,innerWidth,outterWidth 及事件操作
- JS技巧
- jQuery.html() .text()和.val() 的用法
- Hadoop 2.0 Yarn代码:NodeManager端代码分析_NM端各服务模块的启动