一、实现记住密码功能

  • 利用上一节的内容,我们来实现一个记住密码的功能,我们直接修改BroadcastBestPractice项目中的代码。
  • 首先修改login.xm中的代码
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="1" >
    .........省略代码.............

	<TableRow>
	    <CheckBox
	        android:id="@+id/remember_pass"
	        android:layout_height="wrap_content" />
	    
	    <TextView
	        android:layout_height="wrap_content"
	        android:text="Remember password" />
	    
	</TableRow>
	
	<TableRow>
	    <Button 
	        android:id="@+id/login"
	        android:layout_height="wrap_content"
	        android:layout_span="2"
	        android:text="Login" />

	</TableRow>

</TableLayout>
  • 这里使用到一个新的控件CheckBox,这是一个复选框控件,用户可以通过点击来实现选中和取消,我们就是用这个控件来表示用户是否需要记住密码。
  • 修改LoginActivity中的代码
package com.example.broadcastbestpractice;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
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 BaseActivity{
	
	private SharedPreferences pref;
	
	private Editor editor;
	
	private CheckBox rememberPass;
	
	private EditText accountEdit;
	
	private EditText passwordEdit;
	
	private Button login;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		pref = PreferenceManager.getDefaultSharedPreferences(this);
		accountEdit = (EditText)findViewById(R.id.account);
		rememberPass = (CheckBox)findViewById(R.id.remember_pass);
		boolean isRemember = pref.getBoolean("remember_password", false);
		passwordEdit = (EditText)findViewById(R.id.password);
		login = (Button) findViewById(R.id.login);
		
		if(isRemember) {
			//将账号和密码都设置到文本框中
			String account = pref.getString("account", "");
			String password = pref.getString("password", "");
			accountEdit.setText(account);
			passwordEdit.setText(password);
			rememberPass.setText(true);
		}
		
		
		
		
		login.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String account =accountEdit.getText().toString();
				String password = passwordEdit.getText().toString();
				//如果账号是admin,密码是12345,就认为登录成功
				if(account.equals("admin") && password.equals("12345")) {
					editor = pref.edit();
					if(rememberPass.isChecked()) {
						//检查复选框是否被选中
						editor.putBoolean("remember_password", true);
						editor.putString("account",account);
						editor.putString("password", password);
					}else {
						editor.clear();
					}
					editor.commit();
					
					Intent intent = new Intent(LoginActivity.this,MainActivity.class);
					startActivity(intent);
					finish();
				}else {
					Toast.makeText(LoginActivity.this,"account or password is invalid",Toast.LENGTH_SHORT).show();
				}
			}
			
		});
		
	}

}

32.1

  • 首先获取到SharedPrefereces对象,然后调用了getBoolean()方法获取remember_password这个键对应的值,一开始当然不存在,那么就会使用默认值false,这样就什么都不会发生,接着在登录成功之后会调用CheckBox的isCreated()方法来检查复选框是否被选中。如果选中了说明了用户想要记住密码,这时候将remember_password设置为true,然后把account和password对应的值都存入到SharedPerences文件中并且提交,如果没有选中的话,那么就会调用clear()方法,将刚才输入的账户和密码的文本删除一下。
  • 注意:这样存储的密码是明文的,这是非常不安全的,正式项目会使用加密算法对密码进行加密。

二、SQLite数据存储

  • 安卓内置了SQLite这种轻量级数据库,支持SQL语言以及ACID事务,可以存储结构复杂的数据,之前的两种方式都是很简单的方式,远不能满足我们的要求。
  • 具体使用方式我们下次再说

三、源码:

posted on 2020-09-12 23:29  心悦君兮君不知-睿  阅读(359)  评论(0编辑  收藏  举报