记住密码,自动登录功能实现

本文介绍如何在Android应用中实现记住密码和自动登录的功能。通过使用SharedPreferences存储用户信息,当用户勾选记住密码和自动登录时,应用能够自动填充用户名和密码,并在启动时直接进入主界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

初步代码,功能正在逐步完善。(本章代码莫名多次被吞,本章若部分代码不可见,详细代码可见文章列表第一篇)
1.activity_mian.xml
   <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bground"
    android:orientation="vertical" >
 <Button 
     android:layout_x="70dp" 
     android:layout_height="wrap_content"
  android:id="@+id/login_enter" 
  android:text="登录" 
  android:layout_width="wrap_content"
  android:layout_y="325dp" 
  android:textSize="15dp" 
  android:textColor="#FA6289"></Button>
 <Button 
     android:layout_x="180dp" 
     android:layout_height="wrap_content"
  android:id="@+id/login_reg" 
  android:text="注册" 
  android:layout_width="wrap_content"
  android:layout_y="325dp" 
  android:textSize="15dp" 
  android:textColor="#569D01"></Button>
 <TextView 
     android:textSize="15dp" 
     android:textColor="#EF6D00"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:id="@+id/textView2" 
  android:text="密码:" 
  android:layout_x="46dp"
  android:layout_y="134dp"></TextView>

 <TextView
     android:id="@+id/textView1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_x="46dp"
     android:layout_y="78dp"
     android:text="用户名:"
     android:textColor="#EF6D00"
     android:textSize="15dp" />

 <CheckBox
     android:id="@+id/login_check1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_x="97dp"
     android:layout_y="194dp"
     android:checked="true"
     android:text="记住密码" />

 <CheckBox
     android:id="@+id/login_check2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_x="96dp"
     android:layout_y="242dp"
     android:text="自动登录" />

 <EditText
     android:id="@+id/login_name"
     android:layout_width="104dp"
     android:layout_height="64dp"
     android:layout_x="127dp"
     android:layout_y="41dp"
     android:ems="10"
     android:hint="输入用户名"
     android:textColor="#569D01" />

 <EditText
     android:id="@+id/login_pass"
     android:layout_width="106dp"
     android:layout_height="wrap_content"
     android:layout_x="124dp"
     android:layout_y="127dp"
     android:ems="10"
     android:hint="输入密码"
     android:inputType="textPassword"
     android:textColor="#569D01" >

     <requestFocus />
 <pre>

2.logo.xml

<img src="https://img-blog.youkuaiyun.com/20150626092754258?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbXpwc2lsZW5jZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:background="@drawable/bground"  
   android:orientation="vertical" >  
   
    
  
   <RelativeLayout  
       android:layout_width="fill_parent"  
        android:layout_height="wrap_content"   
        android:layout_weight="3">  
  
        <ProgressBar  
            android:id="@+id/pgBar"  
            android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_centerInParent="true" />  
  
       <TextView  
            android:id="@+id/tv1"  
           android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
           android:layout_below="@id/pgBar"  
           android:layout_centerHorizontal="true"  
           android:text="正在登录..."  
        android:textColor="#EF6D00"
            android:textSize="18sp" />  
    </RelativeLayout>  
 
   <LinearLayout  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
       android:layout_weight="1"  
        android:gravity="center"  
        android:orientation="vertical" >  
  
       <Button  
           android:id="@+id/btn_back"  
            android:layout_width="70dip"  
            android:layout_height="35dip"  
            android:text="取消"  
          android:textColor="#EF6D00"
            android:textSize="12sp" />  
    </LinearLayout>  
 
  
</LinearLayout>
 
<span style="color:#ff0000;">3.welcome.xml</span>
<span style="color:#ff0000;"><img src="https://img-blog.youkuaiyun.com/20150626092819563?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbXpwc2lsZW5jZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
</span>
<p>package com.example.jiemian;</p><p> import android.app.Activity;  
 import android.os.Bundle;  
   
 public class WelcomeActivity extends Activity {  
   
     @Override  
     protected void onCreate(Bundle savedInstanceState) {  
         // TODO Auto-generated method stub   
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.welcome);  
    }  </p><p>
}
</p>
<span style="color:#ff0000;"><strong>4.MainActivity.java</strong></span>

<pre name="code" class="java">package com.example.jiemian;

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.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;    
public class MainActivity extends Activity {  
     
   private EditText userName, password;  
   private CheckBox rem_pw, auto_login;  
   private Button btn_login;  
    private ImageButton btnQuit;  
    private String userNameValue,passwordValue;  
   private SharedPreferences sp; 
  
  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
        setContentView(R.layout.activity_main);  
            
       //获得实例对象   
        sp = this.getSharedPreferences("userInfo", 0);  
       userName = (EditText) findViewById(R.id.login_name);  
       password = (EditText) findViewById(R.id.login_pass);  
       rem_pw = (CheckBox) findViewById(R.id.login_check1);  
       auto_login = (CheckBox) findViewById(R.id.login_check2);  
        btn_login = (Button) findViewById(R.id.login_enter);  
       btnQuit = (ImageButton)findViewById(R.id.btn_back);  
          
     if(sp.getBoolean("ISCHECK", false))  
        {  
         //设置默认是记录密码状态   
          rem_pw.setChecked(true);  
         userName.setText(sp.getString("USER_NAME", ""));  
          password.setText(sp.getString("PASSWORD", ""));  
         //判断自动登陆多选框状态   
         if(sp.getBoolean("AUTO_ISCHECK", false))  
          {  
                //设置默认是自动登录状态   
                auto_login.setChecked(true);  
               //跳转界面   
                Intent intent = new Intent(MainActivity.this,LogoActivity.class);  
               MainActivity.this.startActivity(intent);  
                  
         }  
        }  
         
      //现在默认为用户名为:ming 密码:123456   
       btn_login.setOnClickListener(new OnClickListener() {  
 
           public void onClick(View v) {  
                userNameValue = userName.getText().toString();  
               passwordValue = password.getText().toString();  
                 
               if(userNameValue.equals("ming")&&passwordValue.equals("123456"))  
               {  
                    Toast.makeText(MainActivity.this,"登录成功", Toast.LENGTH_SHORT).show();  
                   //登录成功和记住密码框为选中状态才保存用户信息   
                   if(rem_pw.isChecked())  
                    {  
                   //记住用户名、密码、   
                     Editor editor = sp.edit();  
                   editor.putString("USER_NAME", userNameValue);  
                     editor.putString("PASSWORD",passwordValue);  
                      editor.commit();  
                   }  
                    //跳转界面   
                    Intent intent = new Intent(MainActivity.this,LogoActivity.class);  
                    MainActivity.this.startActivity(intent);  
                    //finish();   
                      
                }else{  
                     
                    Toast.makeText(MainActivity.this,"用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show();  
                }  
                 
           }  
        });  
       rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {  
                if (rem_pw.isChecked()) {  
                     
                    System.out.println("记住密码已选中");  
                   sp.edit().putBoolean("ISCHECK", true).commit();  
                     
                }else {  
                     
                   System.out.println("记住密码没有选中");  
                    sp.edit().putBoolean("ISCHECK", false).commit();  
                     
               }  
  
            }  
       });  
          
        auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {  
               if (auto_login.isChecked()) {  
                    System.out.println("自动登录已选中");  
                    sp.edit().putBoolean("AUTO_ISCHECK", true).commit();  
  
                } else {  
                    System.out.println("自动登录没有选中");  
                    sp.edit().putBoolean("AUTO_ISCHECK", false).commit();  
               }  
           }  
        });  
          
        /*btnQuit.setOnClickListener(new OnClickListener() {  
              
          public void onClick(View v) {  
              finish();  
           }  
      });  */
 
   }
}

5.LogoActivity.java

<pre name="code" class="java">package com.example.jiemian;
	import android.widget.ProgressBar;  
	import android.app.Activity;  
	import android.content.Intent;  
	import android.content.SharedPreferences;  
	import android.content.SharedPreferences.Editor;  
	import android.opengl.ETC1;  
	import android.os.Bundle;  
	import android.view.View;  
	import android.view.View.OnClickListener;  
	import android.view.Window;  
	import android.view.animation.AlphaAnimation;  
	import android.view.animation.Animation;  
	import android.view.animation.Animation.AnimationListener;  
	import android.widget.Button;  
	import android.widget.ImageButton;  
	public class LogoActivity extends Activity {  
	    private ProgressBar progressBar;  
	    private Button backButton;  
	  
	    protected void onCreate(Bundle savedInstanceState) {  
	       super.onCreate(savedInstanceState);    
	       this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
	       setContentView(R.layout.logo);  
	 
	       progressBar = (ProgressBar) findViewById(R.id.pgBar);  
	       backButton = (Button) findViewById(R.id.btn_back);  
	 
	       Intent intent = new Intent(this, WelcomeActivity.class);  
	        LogoActivity.this.startActivity(intent);  
	  
	        backButton.setOnClickListener(new OnClickListener() {  
	
	           public void onClick(View v) {  
               finish();  

	           }  
       });  
	 
   }  
	 
	}  



6.WelcomeActivity.java

<pre name="code" class="java">package com.example.jiemian;

	import android.app.Activity;  
	import android.os.Bundle;  
	  
	public class WelcomeActivity extends Activity {  
	  
	    @Override  
	    protected void onCreate(Bundle savedInstanceState) {  
	        // TODO Auto-generated method stub   
	        super.onCreate(savedInstanceState);  
	        setContentView(R.layout.welcome);  
    }  


}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值