SharedPreferences的介绍
来源:互联网 发布:windows udp端口测试 编辑:程序博客网 时间:2024/06/04 18:58
SharedPreferences的用法简介:
1.SharedPreferences是Android中用于实现存储方式的技术。SharedPreferences的使用非常简单,能够轻松的存放数据和读取数据。SharedPreferences只能保存简单类型的数据,例如,String、int等。一般会将复杂类型的数据转换成Base64编码,然后将转换后的数据以字符串的形式保存在XML文件中,再用SharedPreferences保存。
(1)使用Activity类的getSharedPreferences方法获得SharedPreferences对象,其中存储key-value的文件的名称由getSharedPreferences方法的第一个参数指定。
(2)使用SharedPreferences接口的edit获得SharedPreferences.Editor对象。
(3)通过SharedPreferences.Editor接口的putXxx方法保存key-value对。其中Xxx表示不同的数据类型。例如:字符串类型的value需要用putString方法。
(4)通过SharedPreferences.Editor接口的commit方法保存key-value对。commit方法相当于数据库事务中的提交(commit)操作。
2.实例(用SharedPreferences实现登陆页面记住密码和自动登录)
MainActivity.java 代码:
- "font-family:'Courier
New';" >packagecom.liu.activity; -
- public
class LoginActivity extendsActivity { -
-
private EditText userName, password; -
private CheckBox rem_pw, auto_login; -
private Button btn_login; -
private ImageButton btnQuit; -
private String userNameValue,passwordValue; -
private SharedPreferences sp; -
-
public void onCreate(Bundle savedInstanceState) { -
super.onCreate(savedInstanceState); -
-
//去除标题 -
this.requestWindowFeature(Window.FEATURE_NO_TITLE); -
setContentView(R.layout.login); -
-
//获得实例对象 -
sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE); -
userName = (EditText) findViewById(R.id.et_zh); -
password = (EditText) findViewById(R.id.et_mima); -
rem_pw = (CheckBox) findViewById(R.id.cb_mima); -
auto_login = (CheckBox) findViewById(R.id.cb_auto); -
btn_login = (Button) findViewById(R.id.btn_login); -
btnQuit = (ImageButton)findViewById(R.id.img_btn); -
-
-
//判断记住密码多选框的状态 -
if(sp.getBoolean("ISCHECK", false)) -
{ -
//设置默认是记录密码状态 -
rem_pw.setChecked(true); -
userName.setText(sp.getString("USER_NAME", "")); -
password.setText(sp.getString("PASSWORD", "")); -
//判断自动登陆多选框状态 -
if(sp.getBoolean("AUTO_ISCHECK", false)) -
{ -
//设置默认是自动登录状态 -
auto_login.setChecked(true); -
//跳转界面 -
Intent intent = new Intent(LoginActivity. this,LogoActivity.class); -
LoginActivity.this.startActivity(intent); -
-
} -
} -
-
// 登录监听事件 现在默认为用户名为:liu 密码:123 -
btn_login.setOnClickListener(new OnClickListener() { -
-
public void onClick(View v) { -
userNameValue = userName.getText().toString(); -
passwordValue = password.getText().toString(); -
-
if(userNameValue.equals("liu")&&passwordValue.equals("123")) -
{ -
Toast.makeText(LoginActivity.this,"登录成功", Toast.LENGTH_SHORT).show(); -
//登录成功和记住密码框为选中状态才保存用户信息 -
if(rem_pw.isChecked()) -
{ -
//记住用户名、密码、 -
Editor editor = sp.edit(); -
editor.putString("USER_NAME", userNameValue); -
editor.putString("PASSWORD",passwordValue); -
editor.commit(); -
} -
//跳转界面 -
Intent intent = new Intent(LoginActivity. this,LogoActivity.class); -
LoginActivity.this.startActivity(intent); -
//finish(); -
-
}else{ -
-
Toast.makeText(LoginActivity.this,"用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show(); -
} -
-
} -
}); -
-
//监听记住密码多选框按钮事件 -
rem_pw.setOnCheckedChangeListen er(new OnCheckedChangeListener() { -
public void onCheckedChanged(CompoundButton booleanbuttonView, isChecked) { -
if (rem_pw.isChecked()) { -
-
System.out.println("记住密码已选中"); -
sp.edit().putBoolean("ISCHECK", true).commit(); -
-
}else { -
-
System.out.println("记住密码没有选中"); -
sp.edit().putBoolean("ISCHECK", false).commit(); -
-
} -
-
} -
}); -
-
//监听自动登录多选框事件 -
auto_login.setOnCheckedChangeListen er(new OnCheckedChangeListener() { -
public void onCheckedChanged(CompoundButton booleanbuttonView, isChecked) { -
if (auto_login.isChecked()) { -
System.out.println("自动登录已选中"); -
sp.edit().putBoolean("AUTO_ISCHECK", true).commit(); -
-
} else { -
System.out.println("自动登录没有选中"); -
sp.edit().putBoolean("AUTO_ISCHECK", false).commit(); -
} -
} -
}); -
-
btnQuit.setOnClickListener(new OnClickListener() { -
-
@Override -
public void onClick(View v) { -
finish(); -
} -
}); -
-
} - }
activity.xml 文件:
- <span
style="font-family:'Courier New';" ><?xmlversion="1.0" encoding="utf-8"?> - <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" -
android:layout_width="fill_parent" -
android:layout_height="fill_parent" -
android:background="@drawable/logo_bg" -
android:orientation="vertical" > -
-
<RelativeLayout -
android:layout_width="fill_parent" -
android:layout_height="wrap_content" > -
<ImageButton -
android:id="@+id/img_btn" -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:layout_alignParentRight="true" -
android:background="@drawable/quit"/> -
-
<TextView -
android:id="@+id/tv_zh" -
android:layout_width="wrap_content" -
android:layout_height="35dip" -
android:layout_marginLeft="12dip" -
android:layout_marginTop="10dip" -
android:gravity="bottom" -
android:text="帐号:" -
android:textColor="#000000" -
android:textSize="18sp" /> -
-
<EditText -
android:id="@+id/et_zh" -
android:layout_width="fill_parent" -
android:layout_height="40dip" -
android:layout_below="@id/tv_zh" -
android:layout_marginLeft="12dip" -
android:layout_marginRight="10dip" /> -
-
<TextView -
android:id="@+id/tv_mima" -
android:layout_width="wrap_content" -
android:layout_height="35dip" -
android:layout_below="@id/et_zh" -
android:layout_marginLeft="12dip" -
android:layout_marginTop="10dip" -
android:gravity="bottom" -
android:text="密码:" -
android:textColor="#000000" -
android:textSize="18sp" /> -
-
<EditText -
android:id="@+id/et_mima" -
android:layout_width="fill_parent" -
android:layout_height="40dip" -
android:layout_below="@id/tv_mima" -
android:layout_marginLeft="12dip" -
android:layout_marginRight="10dip" -
android:maxLines="200" -
android:password="true" -
android:scrollHorizontally="true" /> -
-
<CheckBox -
android:id="@+id/cb_mima" -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:layout_below="@id/et_mima" -
android:layout_marginLeft="12dip" -
android:text="记住密码" -
android:textColor="#000000" /> -
-
<CheckBox -
android:id="@+id/cb_auto" -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:layout_below="@id/cb_mima" -
android:layout_marginLeft="12dip" -
android:text="自动登录" -
android:textColor="#000000" /> -
<Button -
android:id="@+id/btn_login" -
android:layout_width="80dip" -
android:layout_height="40dip" -
android:layout_below="@id/et_mima" -
android:layout_alignParentRight="true" -
android:layout_alignTop="@id/cb_auto" -
android:layout_marginRight="10dip" -
android:gravity="center" -
android:text="登录" -
android:textColor="#000000" -
android:textSize="18sp"/> -
-
-
</RelativeLayout> -
-
-
- </LinearLayout></span>
0 0
- SharedPreferences的介绍
- sharedPreferences的相关用法介绍
- SharedPreferences介绍
- SharedPreferences介绍:
- SharedPreferences介绍
- SharedPreferences介绍
- SharedPreferences介绍
- SharedPreferences 介绍
- SharedPreferences介绍
- Android的SharedPreferences的使用介绍
- Android的SharedPreferences的使用介绍
- android的SharedPreferences介绍和使用方法
- 关于Android中的SharedPreferences的介绍
- Android存储---SharedPreferences的介绍与使用
- android SharedPreferences介绍
- android之SharedPreferences介绍
- SharedPreferences简要介绍
- SharedPreferences简单介绍
- poj3304-Segments-判断直线和线段是否相交
- uCGUI 按键切换对话框内子控件焦点
- 课程实训(猜猜看游戏)进度四
- [从零开始学libGDX]第一章:libGDX游戏引擎简介
- 黑马程序员————Java基础日常笔记---对集合的理解与总结一
- SharedPreferences的介绍
- NS2 trace文件分析脚本(适合无线trace)
- 【Linux】存储映射IO-mmap
- 大数据之storm入门
- Visual C++ Tips: fatal error LNK1276: invalid directive 'ComMode.Obj' found; does not start with '/'
- POJ-3279(Fliptile)--简单搜索
- 我持续推动Rust语言支持Windows XP系统
- 排序算法总结----快速排序
- Git常用命令