首页

android 记住用户名密码+自动登录示例源码

java

2020-7-1

package com.example.autologin;

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

public class LoginActivity extends Activity {
	private EditText username;
	private EditText userpassword;
	private CheckBox remember;
	private CheckBox autologin;
	private Button login;
	private SharedPreferences sp;
	private String userNameValue,passwordValue;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		
		// 初始化用户名、密码、记住密码、自动登录、登录按钮
		username = (EditText) findViewById(R.id.username);
		userpassword = (EditText) findViewById(R.id.userpassword);
		remember = (CheckBox) findViewById(R.id.remember);
		autologin = (CheckBox) findViewById(R.id.autologin);
		login = (Button) findViewById(R.id.login);

		sp = getSharedPreferences("userInfo", 0);
		String name=sp.getString("USER_NAME", "");
		String pass =sp.getString("PASSWORD", "");
		

        boolean choseRemember =sp.getBoolean("remember", false);
        boolean choseAutoLogin =sp.getBoolean("autologin", false);
  //      Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
        
        //如果上次选了记住密码,那进入登录页面也自动勾选记住密码,并填上用户名和密码
        if(choseRemember){
        	username.setText(name);
        	userpassword.setText(pass);
        	remember.setChecked(true);
        }
        //如果上次登录选了自动登录,那进入登录页面也自动勾选自动登录
        if(choseAutoLogin){
        	autologin.setChecked(true);
        }
        
		
		
		login.setOnClickListener(new OnClickListener() {
		
			// 默认可登录帐号tinyphp,密码123
			@Override
			public void onClick(View arg0) {
				userNameValue = username.getText().toString();
				passwordValue = userpassword.getText().toString();
				SharedPreferences.Editor editor =sp.edit();
				
				// TODO Auto-generated method stub
				if (userNameValue.equals("tinyphp")
						&& passwordValue.equals("123")) {
					Toast.makeText(LoginActivity.this, "登录成功",
							Toast.LENGTH_SHORT).show();
					
					//保存用户名和密码
					editor.putString("USER_NAME", userNameValue);
					editor.putString("PASSWORD", passwordValue);
					
					//是否记住密码
					if(remember.isChecked()){						
						editor.putBoolean("remember", true);						
					}else{
						editor.putBoolean("remember", false);				
					}
					
															
					//是否自动登录
						if(autologin.isChecked()){							
							editor.putBoolean("autologin", true);							
						}else{
							editor.putBoolean("autologin", false);
						}
					editor.commit();
						
					//跳转
					Intent intent =new Intent(LoginActivity.this,SuccessActivity.class);
					startActivity(intent);
				} else {
					Toast.makeText(LoginActivity.this, "用户名或密码错误,请重新登录!",
							Toast.LENGTH_SHORT).show();
				}

			}

		});

	}

}
资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
autologin/.classpath , 475
autologin/.project , 845
autologin/.settings/org.eclipse.jdt.core.prefs , 177
autologin/AndroidManifest.xml , 1049
autologin/bin/AndroidManifest.xml , 1049
autologin/bin/autologin.apk , 283547
autologin/bin/classes/com/example/autologin/BuildConfig.class , 351
autologin/bin/classes/com/example/autologin/LoginActivity$1.class , 2938
autologin/bin/classes/com/example/autologin/LoginActivity.class , 3078
autologin/bin/classes/com/example/autologin/MainActivity.class , 2543
autologin/bin/classes/com/example/autologin/R$attr.class , 349
autologin/bin/classes/com/example/autologin/R$dimen.class , 470
autologin/bin/classes/com/example/autologin/R$drawable.class , 416
autologin/bin/classes/com/example/autologin/R$id.class , 683
autologin/bin/classes/com/example/autologin/R$layout.class , 463
autologin/bin/classes/com/example/autologin/R$menu.class , 397
autologin/bin/classes/com/example/autologin/R$string.class , 481
autologin/bin/classes/com/example/autologin/R$style.class , 440
autologin/bin/classes/com/example/autologin/R.class , 703
autologin/bin/classes/com/example/autologin/SuccessActivity.class , 711
autologin/bin/classes.dex , 697464
autologin/bin/dexedLibs/android-support-v4-50fed096bba38101cb6d3215a6f85b52.jar , 234431
autologin/bin/jarlist.cache , 120
autologin/bin/res/crunch/drawable-hdpi/ic_launcher.png , 5964
autologin/bin/res/crunch/drawable-mdpi/ic_launcher.png , 3112
autologin/bin/res/crunch/drawable-xhdpi/ic_launcher.png , 9355
autologin/bin/res/crunch/drawable-xxhdpi/ic_launcher.png , 17889
autologin/bin/resources.ap_ , 42643
autologin/gen/com/example/autologin/BuildConfig.java , 163
autologin/gen/com/example/autologin/R.java , 3176
autologin/ic_launcher-web.png , 51394
autologin/libs/android-support-v4.jar , 627582
autologin/proguard-project.txt , 781
autologin/project.properties , 563
autologin/res/drawable-hdpi/ic_launcher.png , 7658
autologin/res/drawable-mdpi/ic_launcher.png , 3777
autologin/res/drawable-xhdpi/ic_launcher.png , 12516
autologin/res/drawable-xxhdpi/ic_launcher.png , 24777
autologin/res/layout/home.xml , 930
autologin/res/layout/login.xml , 1624
autologin/res/layout/success.xml , 509
autologin/res/menu/main.xml , 371
autologin/res/values/dimens.xml , 220
autologin/res/values/strings.xml , 233
autologin/res/values/styles.xml , 697
autologin/res/values-v11/styles.xml , 334
autologin/res/values-v14/styles.xml , 391
autologin/res/values-w820dp/dimens.xml , 381
autologin/src/com/example/autologin/LoginActivity.java , 3262
autologin/src/com/example/autologin/MainActivity.java , 1290
autologin/src/com/example/autologin/SuccessActivity.java , 419
没有账号? 忘记密码?

社交账号快速登录