您的位置:首页 > 移动开发 > Android开发

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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息