SharedPreference控件实现记住密码,自动登录例子

来源:互联网 发布:两个矩阵正交什么意思 编辑:程序博客网 时间:2024/05/29 08:06
SharedPreferences介绍:
SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,这里我们用来保存用户名、密码等.
SharedPreferences用法:
        name:配置参数保存的名字,没有就创建。
        mode:保存方式,分一下几种:
  •        Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
  •        Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.
  •        Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件.
  •        MODE_WORLD_READABLE:表示当前文件可以被其他应用读取. MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入.
实验结果截图:

工程目录:

代码:LoginActivity.class :

com.example.minitwittersimulate;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class LoginActivity extends Activity {private Button btn_login;private EditText username;private EditText password;private CheckBox chk_pw;private CheckBox chk_dl;private String usernameStr;private String passwordStr;SharedPreferences sp;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//去除标题requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);//示例对象sp=getSharedPreferences("userinfo",Context.MODE_PRIVATE);btn_login=(Button) findViewById(R.id.btnLogin);username=(EditText) findViewById(R.id.etUsername);password=(EditText) findViewById(R.id.etPassword);chk_pw=(CheckBox) findViewById(R.id.chk_pw);chk_dl=(CheckBox) findViewById(R.id.chk_dl);//判断是否记住密码:if(sp.getBoolean("ISCHECKED",false))//这句中前后两个参数的意思:前面是关键字,后面的是如果没有默认false;{//默认密码状态chk_pw.setChecked(true);username.setText(sp.getString("USERNAME",""));password.setText(sp.getString("PASSWORD", ""));//判断自动登录状态if(sp.getBoolean("ZIDONG", false)){//默认登录状态chk_dl.setChecked(true);//跳转页面Intent intent=new Intent(LoginActivity.this,Activity_Context.class);startActivity(intent);finish();}}//登录写入事件btn_login.setOnClickListener(new OnClickListener() {public void onClick(View view) {usernameStr=username.getText().toString();passwordStr=password.getText().toString();//用户名&&密码不为空 默认为链接正确if(!usernameStr.equals("")&&!passwordStr.equals("")){Toast.makeText(LoginActivity.this, "OK"+usernameStr+" "+passwordStr, Toast.LENGTH_LONG).show();//记住密码&&自动登录 选中是保存用户信息if(chk_pw.isChecked()){ Editor edt=sp.edit(); edt.putString("USERNAME", usernameStr); edt.putString("PASSWORD", passwordStr);// edt.putBoolean("ISCHECKED", true); edt.commit();}//if(chk_dl.isChecked()){//sp.edit().putBoolean("ZIDONG", true);//sp.edit().commit();//}//跳转页面Intent intent=new Intent(LoginActivity.this,Activity_Context.class);startActivity(intent);finish();}else{Toast.makeText(LoginActivity.this, "用户名、密码不能为空", Toast.LENGTH_LONG).show();}}});//记住密码多选框按钮chk_pw.setOnClickListener(new OnClickListener() {public void onClick(View arg0) {if(chk_pw.isChecked()){sp.edit().putBoolean("ISCHECKED", true).commit();}else{sp.edit().putBoolean("ISCHECKED", false).commit();}}});chk_dl.setOnClickListener(new OnClickListener() {public void onClick(View arg0) {if(chk_dl.isChecked()){sp.edit().putBoolean("ZIDONG", true).commit();}else{sp.edit().putBoolean("ZIDONG", false).commit();}}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
Activity_Context.class:
package com.example.minitwittersimulate;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;import android.view.Menu;import android.view.View;import android.widget.Toast;public class Activity_Context extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_activity__context);}public void click1(View v){Toast.makeText(Activity_Context.this, "ok", Toast.LENGTH_LONG).show();//取消自动登录SharedPreferences sp = getSharedPreferences("userinfo",MODE_PRIVATE);sp.edit().putBoolean("ZIDONG", false).commit();//返回首页Intent intent=new Intent(Activity_Context.this,LoginActivity.class);startActivity(intent);finish();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity__context, menu);return true;}}

资源相关下载:

http://download.csdn.net/download/yyd_diablo/8773227(项目)

  • 点击打开链接


0 0
原创粉丝点击