SharedPreferences的用法

package com.example.login;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.login.service.LoginService;

public class MainActivity extends Activity {
	private EditText et_username;
	private EditText et_password;
	private CheckBox cb;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		et_username = (EditText) findViewById(R.id.ed_username);
		et_password = (EditText) findViewById(R.id.ed_password);
		cb = (CheckBox)findViewById(R.id.cb);
		SharedPreferences sp = getSharedPreferences("config", Context.MODE_PRIVATE);
		String username = sp.getString("username", "");
		et_username.setText(username);
		String password = sp.getString("password", "");
		et_password.setText(password);
	}
	
	public void login(View view) {
		String username = et_username.getText().toString().trim();
		String password = et_password.getText().toString().trim();
		if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
			Toast.makeText(this, "用户名和密码不能为空", 0).show();
		} 
		else if ("zhangsan".equals(username) && "123".equals(password)){
			//如果不为空记住密码
			LoginService  loginService = new LoginService();
			if (cb.isChecked()) {
				loginService.savaUserInfo(this, username, password);
				Toast.makeText(this, "保存成功", 0).show();
			} 
		}
		else 
			Toast.makeText(this, "登入失败", 0).show();
	}
}


package com.example.login.service;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class LoginService {
	public  void savaUserInfo(Context context, String username, String password) {
		//第一参数为要在data/data/包名的Shared_prefs目录新建一个为xml的文件commit这个文件就会被创建,第二参数为私有模式
		SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
		//得到一个sp的编辑器
		Editor editor = sp.edit();
		editor.putString("username", username);
		editor.putString("password", password);
		editor.putBoolean("open", true);
		editor.putInt("count", 25);
		editor.putFloat("pir", 3.141592f);
		//保证数据同时被提交
		editor.commit();
		
	}
}
 

全部代码:

package com.example.login;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.login.service.LoginService;

public class MainActivity extends Activity {
	private EditText et_username;
	private EditText et_password;
	private CheckBox cb;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		et_username = (EditText) findViewById(R.id.ed_username);
		et_password = (EditText) findViewById(R.id.ed_password);
		cb = (CheckBox)findViewById(R.id.cb);
		SharedPreferences sp = getSharedPreferences("config", Context.MODE_PRIVATE);
		String username = sp.getString("username", "");
		et_username.setText(username);
		String password = sp.getString("password", "");
		et_password.setText(password);
	}
	
	public void login(View view) {
		String username = et_username.getText().toString().trim();
		String password = et_password.getText().toString().trim();
		if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
			Toast.makeText(this, "用户名和密码不能为空", 0).show();
		} 
		else if ("zhangsan".equals(username) && "123".equals(password)){
			LoginService  loginService = new LoginService();
			if (cb.isChecked()) {
				loginService.savaUserInfo(this, username, password);
				Toast.makeText(this, "保存成功", 0).show();
			} 
		}
		else 
			Toast.makeText(this, "登入失败", 0).show();
	}
}


package com.example.login.service;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class LoginService {
	public  void savaUserInfo(Context context, String username, String password) {
		SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putString("username", username);
		editor.putString("password", password);
		editor.putBoolean("open", true);
		editor.putInt("count", 25);
		editor.putFloat("pir", 3.141592f);
		editor.commit();
		
	}
}



 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值