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();
}
}