android开发系列 qq,Android开发之使用SharedPreferences实现QQ登陆的选项框记忆功能(源代码分享)...

本系列文章由@林泓成出品,转载请注明出处。

根据上篇博客讲的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 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 map=new HashMap();

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 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 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 getMsg(String fileName) {

Map map = null;

//读取数据用不到edit

SharedPreferences preferences = getSharedPreferences(fileName,

Context.MODE_PRIVATE);

map = preferences.getAll();

return map;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值