自动登录SharedPreferences记住密码

//布局文件
<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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:layout_centerInParent="true"
        android:hint="请输入用户名"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pass"
        android:layout_below="@+id/name"
        android:hint="请输入密码"
        />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/box_pass"
        android:layout_below="@+id/pass"
        android:text="记住密码"
        />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/box_zidong"
        android:layout_below="@+id/pass"
        android:layout_marginLeft="100dp"
        android:layout_toRightOf="@+id/box_pass"
        android:text="自动登录"
        />
   
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录按钮"
        android:layout_below="@+id/box_pass"
        android:layout_centerHorizontal="true"
        android:id="@+id/button"
        />

</RelativeLayout>

//主页面代码如下
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class MainActivity extends Activity {

 

 private EditText name1;
 private EditText pass1;
 private CheckBox box_pass;
 private CheckBox box_zidong;
 private Button button;
 private SharedPreferences sharedPreferences;
 private Editor edit;
 private String name;
 private String pass;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  //查找控件
  name1 = (EditText) findViewById(R.id.name);
  pass1 = (EditText) findViewById(R.id.pass);
  box_pass = (CheckBox) findViewById(R.id.box_pass);
  box_zidong = (CheckBox) findViewById(R.id.box_zidong);
  button = (Button) findViewById(R.id.button);
  
  //获取shared
  sharedPreferences = getSharedPreferences("person", MODE_PRIVATE);
  edit = sharedPreferences.edit();
  
  
  //默认的框
  boolean pass_click = sharedPreferences.getBoolean("pass_click", false);
  if(pass_click){
   
   //获取值
   String p_name = sharedPreferences.getString("name", null);
   String p_pass = sharedPreferences.getString("pass", null);
   
   //选中
       box_pass.setChecked(true);
      
       //赋值
       name1.setText(p_name);
       pass1.setText(p_pass);
  
  }
  
  //自动登录的默认点击
  boolean zidong_click = sharedPreferences.getBoolean("zidong_click",false);
  if(zidong_click){
   
   Intent intent = new Intent(MainActivity.this,LoginActivity.class);
   startActivity(intent);
   finish();
  }
  
  
  //登录的按钮
  button.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    //记住密码
    if(box_pass.isChecked()){
     
     //获取值
     name = name1.getText().toString();
     pass = pass1.getText().toString();
     
     //保存至
     edit.putString("name", name);
     edit.putString("pass", pass);
     
     //状态值
     edit.putBoolean("pass_click", true);
     edit.commit();
     
    }
    
    //自动登录的选中
    if(box_zidong.isChecked()){
     
     //状态值
     edit.putBoolean("zidong_click", true);
     edit.commit();
    }
    
    
   //跳转
    Intent intent = new Intent(MainActivity.this,LoginActivity.class);
    //传值
    intent.putExtra("name", name);
    intent.putExtra("pass", pass);
    
    startActivity(intent);
    
    //结束
    finish();
    
   }
  });
 }
}


//注销页面布局文件
<LinearLayout 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"
    android:orientation="vertical"
   >
  
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注销"
        android:id="@+id/button"
        />
   
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="哈哈"
        android:textSize="40sp"
        android:id="@+id/name"
        />
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="哈哈"
        android:textSize="40sp"
        android:id="@+id/pass"
        />

  

</LinearLayout>

//注销页面主页面代码如下

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class LoginActivity extends Activity {

 

 private Button button;
 private TextView name;
 private TextView pass;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_login);
  
  
  //查找控件
  button = (Button) findViewById(R.id.button);
  name = (TextView) findViewById(R.id.name);
  pass = (TextView) findViewById(R.id.pass);
  
  //接受值
  Intent intent = getIntent();
  String name1 = intent.getStringExtra("name");
  String pass1 = intent.getStringExtra("pass");
  
  //赋值
  name.setText(name1);
  pass.setText(pass1);
  
  
  //获取shared
  SharedPreferences sharedPreferences = getSharedPreferences("person", MODE_PRIVATE);
  final Editor editor = sharedPreferences.edit();
  
  //注销的点击事件
  button.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    editor.clear();
    editor.commit();
    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
    startActivity(intent);
    finish();
   }
  });
 }

 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值