android 登陆时记住密码

本文介绍如何在Android应用中实现登录页面的记住密码功能,包括布局设计、代码实现以及数据存储策略。

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

android 登陆时记住密码

1.布局文件.
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.cdc.http.MainActivity$PlaceholderFragment" >

    
    <EditText 
        android:id="@+id/main_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="用户名"
       
        />
    
     <EditText 
        android:id="@+id/main_et2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/main_et"
        android:hint="密码"
        />
     
     
     <CheckBox 
         android:id="@+id/main_checkbox"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@id/main_et2"/>
     
     <TextView 
         android:id="@+id/main_tv"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="记住我"
         android:layout_toRightOf="@id/main_checkbox"
         android:layout_below="@id/main_et2"
         android:layout_alignBottom="@id/main_checkbox"/>
    
    <Button 
        android:id="@+id/main_bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"
        android:layout_below="@id/main_checkbox"/>
    
    
   
 
</RelativeLayout>

2.代码
package com.cdc.http;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
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;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class MainActivity extends Activity {

	private EditText etName;
	private EditText etPassword;

	private CheckBox checkBox;

	private Button denglu;

	private String name;

	private String password;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		etName = (EditText) findViewById(R.id.main_et);

		etPassword = (EditText) findViewById(R.id.main_et2);

		checkBox = (CheckBox) findViewById(R.id.main_checkbox);

		denglu = (Button) findViewById(R.id.main_bt);

		SharedPreferences sharedPreferences = getSharedPreferences("mima",
				Context.MODE_PRIVATE);

		String temp_name = sharedPreferences.getString("name", "");

		String temp_password = sharedPreferences.getString("password", "");

		boolean temp_bl = sharedPreferences.getBoolean("check", false);

		etName.setText(temp_name);

		etPassword.setText(temp_password);

		checkBox.setChecked(temp_bl);

		// 监听用户名输入值的变化
		etName.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				System.out.println("用户名改变了===" + etName.getText().toString());
				
			if(checkBox.isChecked()){

				SharedPreferences sharedPreferences = getSharedPreferences(
						"mima", Context.MODE_PRIVATE);

				Editor editor = sharedPreferences.edit();

				editor.putString("name", etName.getText().toString());
				editor.commit();
			}
			}
		});

		// 监听用户名输入值的变化
		etPassword.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				System.out
						.println("密码改变了===" + etPassword.getText().toString());
				if(checkBox.isChecked()){
				SharedPreferences sharedPreferences = getSharedPreferences(
						"mima", Context.MODE_PRIVATE);

				Editor editor = sharedPreferences.edit();

				editor.putString("password", etPassword.getText().toString());
				editor.commit();
				}
			}
		});

		denglu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				name = etName.getText().toString();

				password = etPassword.getText().toString();

				System.out.println("登录时用户名==" + name + ",登陆时密码==" + password);

			}
		});

		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				// TODO Auto-generated method stub
				System.out.println("点击了checkbox");
				if (arg1) {
					

					SharedPreferences sharedPreferences = getSharedPreferences(
							"mima", Context.MODE_PRIVATE);

					Editor editor = sharedPreferences.edit();

					name = etName.getText().toString();

					password = etPassword.getText().toString();
					editor.putString("name", name);

					editor.putString("password", password);

					editor.putBoolean("check", true);

					editor.commit();
				}else{
					SharedPreferences sharedPreferences = getSharedPreferences(
							"mima", Context.MODE_PRIVATE);

					Editor editor = sharedPreferences.edit();

					name = etName.getText().toString();

					password = etPassword.getText().toString();
					editor.putString("name", "");

					editor.putString("password", "");

					editor.putBoolean("check", false);

					editor.commit();
				}
			}
		});

	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值