2015.1.7,Android项目小案例,SharedPerFrences模仿腾讯QQ记住密码功能

//布局文件


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >


    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"/>
    <EditText
        android:layout_below="@+id/et_name"
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:password="true"
        android:hint="请输入密码"/>
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_password"
        android:orientation="horizontal">
        <CheckBox
            android:id="@+id/cb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住密码"/>
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="100dp"
            android:onClick="click"
            android:text="登陆"/>
      
    </LinearLayout>


</RelativeLayout>

// 存储的工具类

package util;


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


public class SaveData {
public static boolean savaData(Context context,String name,String pwd){
//保存到SharePreFrence中,保存成功返回 true,否则返回false;
//第一个参数是保存的共享参数的名字(自定义的)
//第二个参数是保存共享参数的格式(可追加,私有,可读可写。。。)
SharedPreferences SharedPreferences=context.getSharedPreferences("config", Context.MODE_APPEND);
Editor editor=SharedPreferences.edit();
editor.putString("userName", name);
editor.putString("userPwd", pwd);
editor.commit();
if(editor.commit()){
Toast.makeText(context, "数据保存成功", 0).show();
return true;
}else{
Toast.makeText(context, "数据保存失败", 0).show();
   return false;
}
}


}


//界面代码


package com.example.class3_obj;


import util.SaveData;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {
private EditText mName,mPass;
private CheckBox mCb;
private String name;
private String pass;
private String encodePwd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mName=(EditText) findViewById(R.id.et_name);
        mPass=(EditText) findViewById(R.id.et_password);
        mCb=(CheckBox) findViewById(R.id.cb);
    }
    
    public void click(View v){
    login();
   
   
    }
    
    public void login(){
    name=mName.getText().toString().trim();
    pass=mPass.getText().toString().trim();
    encodePwd=new String(Base64.encode(pass.getBytes(), Base64.DEFAULT));

    if(!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(pass)){
   
    }else{
    Toast.makeText(this, "输入不能为空", 0).show();
    }  
    SharedPreferences sharedPreferences=getSharedPreferences("config", MODE_APPEND);
String saveName=sharedPreferences.getString("userName", "");
String savePwd=sharedPreferences.getString("userPwd", "");
//解密
String jimiPwd=new String(Base64.decode(savePwd.getBytes(), Base64.DEFAULT));
//加密encodePwd=new String(Base64.encode(pass.getBytes(), Base64.DEFAULT));
if(name.equals(saveName)&&pass.equals("123456")){
Toast.makeText(this, jimiPwd+"", 0).show();
Toast.makeText(this, "登陆成功", 0).show();
CheckBoxIstrue();
}else{
Toast.makeText(this, "登陆失败", 0).show();
}
   
    }
    public void CheckBoxIstrue(){
    if(mCb.isChecked()){
boolean flag=SaveData.savaData(this, name, encodePwd);

if(flag){
Toast.makeText(this, "已进入后台记录", 0).show();
}else{
Toast.makeText(this, "后台记录失败", 0).show();
}      
}
    }
    
    @Override
    protected void onStart() {
    SharedPreferences sharedPreferences=getSharedPreferences("config", MODE_APPEND);
    String saveName=sharedPreferences.getString("userName", "");
String savePwd=sharedPreferences.getString("userPwd", "");
mName.setText(saveName);
mPass.setText(savePwd);
    super.onStart();
    }




    @Override
    public 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;
    }
    
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值