轻量级SharedPreferences的存储方式
来源:互联网 发布:淘宝美工主要做些什么 编辑:程序博客网 时间:2024/05/23 23:28
SharedPreferences简介
此功能结合着AutoCompleteTextView控件一时兴起加上的,如果刚开始接触SharedPreferences的朋友完全可以忽略:
SharedPreferences是一个轻量级的的存储方式,通常情况下用于常用的配置,比如说窗体的状态,他提供了android平台常规的long 长整形 整形 字符串,布尔,浮点数等类型,SharedPreferences的存储方式是分为多种权限,以xml方式保存,虽然说此存储方式 比Sqlite效率低,但是对于存储轻量级的数据是比SQLite要好不少的,如果真的存储量不大的话可以考虑此存储方式…
SharedPreferences存储非常简洁主要分为以下几个步骤
- 存入数据
- 通过getSharedPreferences(String fileName,int type );
- 参数:
fileName: 保存的文件名称(当文件存在时不创建)
type: 文件的使用权限(主要是是否与其他应用程序共享) - 功能:获取一个SharedPreferences对象来操作
- 参数:
- 获取编辑器Editor对象
- 通过SharedPreferences的edit()方法获取
- 通过调用编辑器的 put类型(String key,String value) 方法来进行写入数据
- 再调用编辑器的commit()方法进行保存数据保存到硬盘上存储步骤就完成了
- 通过getSharedPreferences(String fileName,int type );
- 读取数据
- 同存入数据的时候一样获取SharedPreferences对象(读取的文件名称一定要一样)
- 调用SharedPreferences中的 get类型(String key,如果取不到值的默认值) 方法来取得已经存入的值
代码块
java代码块:
package cn.edu.bzu.sharedpreferences;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private AutoCompleteTextView actv_UserName;private EditText et_UserPass;private Button btn_SaveUserNameAndPass;private CheckBox cb_IsSaveUserNameAndPass;private Button btn_show;private TextView tv_xianshi;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取控件对象 getFindView(); //为AutoCompleteTextView设置适配器 actv_UserName.setAdapter(adaptation((String)getData().get(0))); //设置提示信息 actv_UserName.setCompletionHint("最近登录的账号!"); //当AutoCompleteTextView获取焦点的时候显示提示内容 actv_UserName.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { AutoCompleteTextView view = (AutoCompleteTextView) v; if (hasFocus) { view.showDropDown(); } } }); //当AutoCompleteTextView控件被点击的时候 actv_UserName.setOnItemClickListener(new autoItemClick()); //当Button被点击的时候 btn_SaveUserNameAndPass.setOnClickListener(new View.OnClickListener() { /** * 当用户点击Button按钮的时候 */ @Override public void onClick(View v) { //当用户点击保存密码的控件后 if(cb_IsSaveUserNameAndPass.isChecked()){ String name=actv_UserName.getText().toString(); String pass=et_UserPass.getText().toString(); //保存数据 setData(name,pass); Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show(); }else{ //当用户取消保存的时候不再保存 setData("",""); Toast.makeText(MainActivity.this, "没有进行保存数据", Toast.LENGTH_SHORT).show(); } actv_UserName.setAdapter(adaptation((String)getData().get(0))); } }); btn_show.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences sp=getSharedPreferences(); /** * Stirng :存入的键值 * String :当读取不出来的时候存入name的默认值 */ String name=sp.getString("userName", null); String pass=sp.getString("userPass", null); tv_xianshi.setText(name+pass); } });}
/**
* 获取控件对象
*/
private void getFindView(){
cb_IsSaveUserNameAndPass=(CheckBox) findViewById(R.id.cb_IsSaveUserNameAndPass);
actv_UserName=(AutoCompleteTextView) findViewById(R.id.actv_UserName);
et_UserPass=(EditText) findViewById(R.id.et_UserPass);
btn_SaveUserNameAndPass=(Button) findViewById(R.id.btn_Save);
cb_IsSaveUserNameAndPass=(CheckBox) findViewById(R.id.cb_IsSaveUserNameAndPass);
btn_show=(Button) findViewById(R.id.btn_show);
tv_xianshi=(TextView) findViewById(R.id.tv_show);
}private SharedPreferences getSharedPreferences(){ /** * String :保存的文件名称(当文件存在时不创建) * int :文件的使用权限(主要是是否与其他应用程序共享) */ return getSharedPreferences("UserData",MODE_APPEND );}@Overridepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true;}/** * 设置AutoCompleteTextView的提示内容 * @return */private ArrayAdapter adaptation(String name){ ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_dropdown_item_1line); adapter.add(name); return adapter;}/** * 获取存储的数据 * @return */private List getData(){ //设置提示内容SharedPreferences sp=getSharedPreferences(); /** * Stirng :存入的键值 * String :当读取不出来的时候存入name的默认值 */ String name=sp.getString("userName", ""); String pass=sp.getString("userPass", ""); List data=new ArrayList(); data.add(name); data.add(pass); return data;}/** * 获取存储的数据 * @param name * @param pass */private void setData(String name,String pass){ SharedPreferences sp=getSharedPreferences(); //获取编辑器 Editor e=sp.edit(); //写入信息 e.putString("userName", name); e.putString("userPass", pass); //保存 e.commit();}/** * 当autocompleteteTextView选项被选择的时候执行的事件 * @author wy * */
class autoItemClick implements OnItemClickListener{
@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { //取出数据 List data=getData(); String name=(String) data.get(0); String pass=(String) data.get(1); //当用户选择前一个账户自动填充密码 et_UserPass.setText(pass);}
}
}
< 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:background=”#FFFACD”
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” >
<TextView android:id="@+id/tv_show" android:layout_width="wrap_content" android:layout_below="@+id/btn_show" android:layout_height="wrap_content" android:layout_marginTop="34dp" /><Button android:id="@+id/btn_show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/show" android:layout_centerVertical="true" android:text="读取数据" /><Button android:id="@+id/btn_Save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="50dp" android:text="登陆" /><CheckBox android:id="@+id/cb_IsSaveUserNameAndPass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/btn_show" android:layout_alignLeft="@+id/btn_show" android:layout_marginBottom="22dp" android:text="保存密码" /><EditText android:id="@+id/et_UserPass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/actv_UserName" android:layout_marginRight="22dp" android:ems="10" android:inputType="textPassword" > <requestFocus /></EditText><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/et_UserPass" android:layout_alignBottom="@+id/et_UserPass" android:layout_alignLeft="@+id/show" android:text="密码:" /><AutoCompleteTextView android:id="@+id/actv_UserName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_UserPass" android:layout_marginLeft="21dp" android:ems="10" /><TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/actv_UserName" android:layout_alignLeft="@+id/tv_show" android:text="账户:" />
- 轻量级SharedPreferences的存储方式
- [Android] SharedPreferences(轻量级的存储方式)
- [Android] SharedPreferences(轻量级的存储方式)
- 轻量级数据存储方式--SharedPreferences
- SharedPreferences(轻量级的存储)-->简单应用
- SharedPreferences(偏好设定;轻量级的存储)
- SharedPreferences轻量级数据存储
- Android sharedpreferences轻量级存储
- SharedPreferences轻量级存储
- 【数据存储】sharedPreferences-轻量级存储
- 数据存储:轻量级存储类SharedPreferences的一个应用实例
- 【SharedPreferences存储】SharedPreferences存储方式
- Android中一个轻量级的存储方案(SharedPreferences)
- Android轻量级的存储SharedPreferences及getSharedPreferences 、 getPreferences 与getDefaultSharedPreferences
- android 简单的存储方式SharedPreferences
- Android数据的存储方式SharedPreferences
- Android 简单的SharedPreferences轻量型存储方式
- 5 SharedPreferences存储方式
- 基于xmpp openfire smack开发之smack类库介绍和使用[2]
- quartus II 报错(Error (10839): Verilog HDL error at sdram_params.v(30): declaring global objects is a)
- SAP BW数据源增强管理
- qt 5 视频播放 ---转载自正文链接
- GRE数学常用备考方法总结
- 轻量级SharedPreferences的存储方式
- Mysql中的sync_binlog参数
- C中的预编译宏定义
- [学习总结]5.25—5.31
- 教你如何迅速秒杀掉:99%的海量数据处理面试题
- PDF分割器的问题
- android 随机生成颜色
- FICO年结完全手册
- Autolayout及VFL经验分享