原创

记住用户密码的登陆

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/modisty/article/details/53056225

一、运行效果图

 

二、核心代码

1.LoginActivity代码:

public class LoginActivity extends Activity {
	private EditText etUsername;
	private EditText etPassword;
	private CheckBox cbRememberPass;
	private Button btnLogin;
	private SharedPreferences sp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_login);
		etUsername = (EditText) findViewById(R.id.etUsername);
		etPassword = (EditText) findViewById(R.id.etPassword);
		cbRememberPass = (CheckBox) findViewById(R.id.cbRememberPass);
		// autologin1 = (CheckBox) findViewById(R.id.autologin1);
		btnLogin = (Button) findViewById(R.id.btnLogin);

		cbRememberPass.setOnClickListener((OnClickListener) this);
		btnLogin.setOnClickListener((OnClickListener) this);

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

		boolean choseRemember = sp.getBoolean("cbRememberPass", false);
		// boolean choseAutoLogin = sp.getBoolean("autologin1", false);
		// 如果上次选了记住密码,那登陆页面也自动勾选记住密码,并填上用户名和密码
		if (choseRemember) {
			etUsername.setText(name);
			etPassword.setText(pass);
			cbRememberPass.setChecked(true);
		}
		// 如果上次登陆选了自动登录,那进入登陆页面也自动勾选自动登录
		/*
		 * if (choseAutoLogin) { autologin1.setChecked(true); }
		 */
		btnLogin.setOnClickListener(new OnClickListener() {
			private String userNameValue;
			private String passwordValue;

			// 默认可登陆账号android,密码123456
			public void onClick(View v) {
				userNameValue = etUsername.getText().toString();
				passwordValue = etPassword.getText().toString();
				SharedPreferences.Editor editor = sp.edit();
				if (userNameValue.equals("android")
						&& passwordValue.equals("123456")) {
					Toast.makeText(LoginActivity.this, "登陆成功",
							Toast.LENGTH_SHORT).show();

					// 保存用户名和密码
					editor.putString("USER_NAME", userNameValue);
					editor.putString("PASSWORD", passwordValue);
					// 是否记住密码
					if (cbRememberPass.isChecked()) {
						editor.putBoolean("cbRememberPass", true);
					} else {
						editor.putBoolean("cbRememberPass", false);
					}

					// 是否自动登陆
					/*
					 * if (autologin1.isChecked()) {
					 * editor.putBoolean("autologin1", true); } else {
					 * editor.putBoolean("autologin1", false); }
					 */
					// editor.commit();
					// 跳转
					Intent intent = new Intent(LoginActivity.this,
							MainActivity.class);
					startActivity(intent);

				} else {
					Toast.makeText(LoginActivity.this, "用户名或密码错误,请重新登陆",
							Toast.LENGTH_SHORT).show();
				}

			}
		});
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


2.MainActivity的核心代码:

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	/*public void go(View v){
		//Intent intent = new Intent(this,LoginActivity.class);
		//startActivity(intent);
		Intent intent = new Intent();
		intent.setClass(MainActivity.this, LoginActivity.class);
		startActivity(intent);
	}
	//点击退出销毁登录记录
	public void out(View v){
		SharedPreferences spout = getSharedPreferences("userInfo",0);
		SharedPreferences.Editor ed = spout.edit();
		ed.clear();
		ed.commit();
		Toast.makeText(this, "销毁记录", Toast.LENGTH_SHORT).show();
	}*/

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

 

三、遇见的问题

     记住密码后,退出再重新进入程序,不能点击用户,自动出现密码,只能用户名和密码同时存在

文章最后发布于: 2016-11-06 20:16:24
展开阅读全文
0 个人打赏

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览