记住密码和自动登录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hubianyu/article/details/82453601
 
 
<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"
    tools:context="${relativePackage}.${activityClass}" >

    <EditText
        android:id="@+id/edit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="55dp"
        android:hint="请输入帐号"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/edit_pass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit_name"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp"
        android:hint="请输入密码"
        android:ems="10" />

    <CheckBox
        android:id="@+id/check_jz"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/edit_pass"
        android:layout_centerVertical="true"
        android:text="记住密码" />

    <CheckBox
        android:id="@+id/check_zd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/check_jz"
        android:layout_alignRight="@+id/edit_pass"
        android:text="自动登录" />

    <Button
        android:id="@+id/but_log"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="76dp"
        android:text="登录" />

</RelativeLayout>

=======

package com.example.sharedpreferences;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

public class MainActivity extends Activity {

   private EditText edit_name,edit_pass;
   private CheckBox check_jz,check_zd;
   private Button but_login;
   private SharedPreferences sharedPreferences;
   private Editor editor;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        
        //1.获取资源ID
        edit_name = (EditText) findViewById(R.id.edit_name);
        edit_pass = (EditText) findViewById(R.id.edit_pass);
        check_jz = (CheckBox) findViewById(R.id.check_jz);
        check_zd = (CheckBox) findViewById(R.id.check_zd);
        but_login = (Button) findViewById(R.id.but_log);
        
        
        //2.得到SharedPreferences
        sharedPreferences = getSharedPreferences("person", MODE_PRIVATE);
        //通过Editor操作SharedPreferences
        editor = sharedPreferences.edit();
        
        //4.取出状态值。判断状态知否为勾选
        boolean ischeck = sharedPreferences.getBoolean("ischeck", false);
        //如果为true直接记住
        if(ischeck){
              //取值
              String names = sharedPreferences.getString("name", null);
              String passs = sharedPreferences.getString("pass", null);
              //将帐号密码 赋值给输入框
              edit_name.setText(names);
              edit_pass.setText(passs);
              //复选框进行勾选
              check_jz.setChecked(true);
        }
        
        //二、取出自动登录的状态值
      boolean ischeck_zd = sharedPreferences.getBoolean("ischeck_zd", false);
      //如果为true直接跳转
      if(ischeck_zd){
         Intent it = new Intent(MainActivity.this,LoginActivity.class);
         startActivity(it);
         finish();
      }
        
        //勾选自动登录同时勾选记住密码
        check_zd.setOnCheckedChangeListener(new OnCheckedChangeListener() {
         
         @Override
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
               check_jz.setChecked(true);
            }else{
               check_jz.setChecked(false);
            }
         }
      });
        
        
        //3.点击登录时 非否勾选了记住密码  
        but_login.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View v) {

            
            //一、判断记住密码
            if(check_jz.isChecked()){
               //得到输入框的内容
               String name = edit_name.getText().toString();
               String pass = edit_pass.getText().toString();
               //存入账号密码  以及状态值
               editor.putString("name", name);
               editor.putString("pass", pass);
               //存入状态值
               editor.putBoolean("ischeck", true);
               editor.commit();
            }
            
            //二、判断自动登录
            if(check_zd.isChecked()){
               //自动登录的状态值
               editor.putBoolean("ischeck_zd", true);
               editor.commit();
            }
            
            
            
            Intent it = new Intent(MainActivity.this,LoginActivity.class);
            startActivity(it);
            finish();
            
         }
      });
        
        
        
        
    }
}

==============

第二个页面

package com.example.sharedpreferences;

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

public class LoginActivity extends Activity {

   private Button button;
   private SharedPreferences sharedPreferences;
   private Editor editor;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_login);
      
      
      //获取资源ID
      button = (Button) findViewById(R.id.button1);
      
      //得到SharedPreferences
      sharedPreferences = getSharedPreferences("person", MODE_PRIVATE);
      editor = sharedPreferences.edit();
   
      
      //点击进行注销
      button.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View v) {
            // TODO Auto-generated method stub
            //清空
            editor.clear();
            editor.commit();
            
            Intent it = new Intent(LoginActivity.this,MainActivity.class);
            startActivity(it);
            finish();
            
         }
      });
      
   }
}

猜你喜欢

转载自blog.csdn.net/hubianyu/article/details/82453601