Android开发之使用SharedPreferences实现QQ登陆的选项框记忆功能(源代码分享)
2014-04-02 18:54
1031 查看
本系列文章由@林泓成出品,转载请注明出处。
根据上篇博客讲的SharedPreferences的简单实现,我们来实现下QQ登陆的时候用户名自动显示以及勾选是否记忆用户名和隐身登陆的功能,通过实例来展现SharedPreferences的实用性。
相关代码如下:
package com.example.f15_sharedpreferences01;
import java.util.HashMap;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class MainActivity extends Activity {
private Button button;
private CheckBox checkBox,checkBox2;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button1);
checkBox=(CheckBox)this.findViewById(R.id.checkBox1);
checkBox2=(CheckBox)this.findViewById(R.id.checkBox2);
editText=(EditText)this.findViewById(R.id.editText1);
Map<String, ?> map=getMsg("login");
if(map!=null&&!map.isEmpty()){
if(map.get("username").toString()!=null&&!map.get("username").toString().equals("")){
editText.setText(map.get("username").toString());
}
checkBox.setChecked( (Boolean) map.get("isname"));
checkBox2.setChecked( (Boolean) map.get("ispwd"));
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
HashMap<String, Object> map=new HashMap<String, Object>();
if(editText.getText().toString().trim().equals("admin")){
if(checkBox.isChecked()){
map.put("username",editText.getText().toString().trim() );
}else{
map.put("username","" );
}
map.put("isname", checkBox.isChecked());
map.put("ispwd", checkBox2.isChecked());
saveMsg("login", map);
}
}
});
}
//写入数据
public boolean saveMsg(String fileName, Map<String, Object> map) {
boolean flag = false;
// 一般Mode都使用private,比较安全
SharedPreferences preferences = getSharedPreferences(fileName,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
// Map类提供了一个称为entrySet()的方法,这个方法返回一个Map.Entry实例化后的对象集。
// 接着,Map.Entry类提供了一个getKey()方法和一个getValue()方法,
// 因此,上面的代码可以被组织得更符合逻辑
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object object = entry.getValue();
// 根据值得不同类型,添加
if (object instanceof Boolean) {
Boolean new_name = (Boolean) object;
editor.putBoolean(key, new_name);
} else if (object instanceof Integer) {
Integer integer = (Integer) object;
editor.putInt(key, integer);
} else if (object instanceof Float) {
Float f = (Float) object;
editor.putFloat(key, f);
} else if (object instanceof Long) {
Long l = (Long) object;
editor.putLong(key, l);
} else if (object instanceof String) {
String s = (String) object;
editor.putString(key, s);
}
}
flag = editor.commit();
return flag;
}
//读取数据
public Map<String, ?> getMsg(String fileName) {
Map<String, ?> map = null;
//读取数据用不到edit
SharedPreferences preferences = getSharedPreferences(fileName,
Context.MODE_PRIVATE);
map = preferences.getAll();
return map;
}
}
根据上篇博客讲的SharedPreferences的简单实现,我们来实现下QQ登陆的时候用户名自动显示以及勾选是否记忆用户名和隐身登陆的功能,通过实例来展现SharedPreferences的实用性。
相关代码如下:
package com.example.f15_sharedpreferences01;
import java.util.HashMap;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class MainActivity extends Activity {
private Button button;
private CheckBox checkBox,checkBox2;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button1);
checkBox=(CheckBox)this.findViewById(R.id.checkBox1);
checkBox2=(CheckBox)this.findViewById(R.id.checkBox2);
editText=(EditText)this.findViewById(R.id.editText1);
Map<String, ?> map=getMsg("login");
if(map!=null&&!map.isEmpty()){
if(map.get("username").toString()!=null&&!map.get("username").toString().equals("")){
editText.setText(map.get("username").toString());
}
checkBox.setChecked( (Boolean) map.get("isname"));
checkBox2.setChecked( (Boolean) map.get("ispwd"));
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
HashMap<String, Object> map=new HashMap<String, Object>();
if(editText.getText().toString().trim().equals("admin")){
if(checkBox.isChecked()){
map.put("username",editText.getText().toString().trim() );
}else{
map.put("username","" );
}
map.put("isname", checkBox.isChecked());
map.put("ispwd", checkBox2.isChecked());
saveMsg("login", map);
}
}
});
}
//写入数据
public boolean saveMsg(String fileName, Map<String, Object> map) {
boolean flag = false;
// 一般Mode都使用private,比较安全
SharedPreferences preferences = getSharedPreferences(fileName,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
// Map类提供了一个称为entrySet()的方法,这个方法返回一个Map.Entry实例化后的对象集。
// 接着,Map.Entry类提供了一个getKey()方法和一个getValue()方法,
// 因此,上面的代码可以被组织得更符合逻辑
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object object = entry.getValue();
// 根据值得不同类型,添加
if (object instanceof Boolean) {
Boolean new_name = (Boolean) object;
editor.putBoolean(key, new_name);
} else if (object instanceof Integer) {
Integer integer = (Integer) object;
editor.putInt(key, integer);
} else if (object instanceof Float) {
Float f = (Float) object;
editor.putFloat(key, f);
} else if (object instanceof Long) {
Long l = (Long) object;
editor.putLong(key, l);
} else if (object instanceof String) {
String s = (String) object;
editor.putString(key, s);
}
}
flag = editor.commit();
return flag;
}
//读取数据
public Map<String, ?> getMsg(String fileName) {
Map<String, ?> map = null;
//读取数据用不到edit
SharedPreferences preferences = getSharedPreferences(fileName,
Context.MODE_PRIVATE);
map = preferences.getAll();
return map;
}
}
相关文章推荐
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- Ubuntu12.04下QQ完美走起啊!走起啊!有木有啊!
- 解決Linux下Android开发真机调试设备不被识别问题
- [Android]在代码里运行另一个程序的方法
- QQ商业化,如何实现从0到1的破局?
- [软件咨询]WPS2012正式版已发布 金山Office移动版4.0发布
- Android笔记-Linux Kernel Ftrace (Function Trace)解析
- 路由器端QQ封堵方案
- QQ输入法自动删除其它输入法的解决方法
- 让普通QQ号也能克隆QQ好友
- VBS取QQ或TM自动登录代码并防止关闭的脚本
- android USB如何修改VID具体实现
- Android增量升级的方法和原理详细介绍
- Android Mouse实现过程详细笔记
- 深入Android Browser配置管理的详解
- Android Mms之:深入理解对话列表管理
- asp.net动态产生checkbox(数据源为DB或内存集合)