模拟记住密码功能

本文介绍了一个简单的Android应用示例,展示了如何使用SharedPreferences保存用户的登录信息,并根据用户的选择自动填充登录字段。此外,还介绍了如何在Activity的生命周期中适当地加载和保存数据。

public class MainActivity extends Activity {
	EditText edName1;
	EditText edName2;
	CheckBox ckName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		edName1 = (EditText) findViewById(R.id.etUsername);
		edName2 = (EditText) findViewById(R.id.etPassword);
		ckName = (CheckBox) findViewById(R.id.checkBox1);
	}
	
	protected void onResume(){
		super.onResume();
		getData();
	}

	private void getData() {
		// TODO Auto-generated method stub
		SharedPreferences sharedPreferences=getSharedPreferences("data", MODE_PRIVATE);
		if(sharedPreferences.getBoolean("change", true)){
			edName1.setText(sharedPreferences.getString("zhanghao", ""));
			edName2.setText(sharedPreferences.getString("key", ""));
			ckName.setChecked(true);
		}else{
			edName1.setText("");
			edName2.setText("");
			ckName.setChecked(false);
		}
		
	}

	public void click(View view) {
		if (!(TextUtils.isEmpty(edName1.getText()))
				&& !(TextUtils.isEmpty(edName2.getText()))) {
			if (edName1.getText().toString().equals("root")
					&& edName2.getText().toString().equals("123456")) {
				if (ckName.isChecked()) {
					save();
					show();
				}else{
					delete();
					show();
				}
			} else {
				Toast.makeText(this, "密码错误", Toast.LENGTH_LONG).show();
			}
		} else {
			Toast.makeText(this, "账号密码不能为空", Toast.LENGTH_LONG).show();
		}

	}

	private void delete() {
		// TODO Auto-generated method stub
		SharedPreferences sharedPreferences = getSharedPreferences("data",
				MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		editor.putString("zhanghao", "");
		editor.putString("key", "");
		editor.putBoolean("change", false);
		editor.commit();
		
	}

	public void show() {
		// TODO Auto-generated method stub
		Intent intent=new Intent(MainActivity.this, ShowActivity.class);
		startActivity(intent);
		
	}

	public void save() {
		String name = edName1.getText().toString();
		String key = edName2.getText().toString();
		SharedPreferences sharedPreferences = getSharedPreferences("data",
				MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		editor.putString("zhanghao", name);
		editor.putString("key", key);
		editor.putBoolean("change", true);
		editor.commit();
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class ShowActivity extends Activity {

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

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.show, menu);
		return true;
	}

}


<think>我们要在Vue中关闭浏览器自带的记住密码功能。浏览器通常会自动提供记住密码功能,但有时出于安全考虑,我们可能需要禁用此功能。 解决方案: 1. 设置输入框的autocomplete属性为"off"或"new-password" 2. 使用隐藏的虚拟输入框来欺骗浏览器(某些浏览器会忽略autocomplete="off") 注意:不同浏览器对autocomplete="off"的处理方式不同,有些会忽略(特别是对于密码字段)。因此,我们需要结合使用多种技巧。 以下是一个示例组件,展示如何禁用浏览器的记住密码功能: </think> # 禁用浏览器记住密码功能 - Vue实现方案 在Vue中禁用浏览器记住密码功能,主要方法是设置输入框的`autocomplete`属性为`off`或`new-password`。我将创建一个完整的登录页面演示如何实现这个功能。 ```html <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>禁用浏览器记住密码功能 - Vue实现</title> <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script> <style> * { box-sizing: border-box; margin: 0; padding: 0; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } body { background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d); min-height: 100vh; display: flex; justify-content: center; align-items: center; padding: 20px; } .container { max-width: 1200px; margin: 0 auto; display: flex; flex-direction: column; align-items: center; } header { text-align: center; margin-bottom: 40px; color: white; text-shadow: 0 2px 4px rgba(0,0,0,0.3); } header h1 { font-size: 2.5rem; margin-bottom: 15px; } header p { font-size: 1.2rem; max-width: 800px; line-height: 1.6; } .content { display: flex; gap: 40px; flex-wrap: wrap; justify-content: center; } .login-container { background: rgba(255, 255, 255, 0.95); border-radius: 16px; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3); width: 400px; padding: 30px; transition: transform 0.3s ease; } .login-container:hover { transform: translateY(-5px); } .login-title { text-align: center; margin-bottom: 25px; color: #2c3e50; font-size: 1.8rem; position: relative; } .login-title:after { content: ""; display: block; width: 60px; height: 4px; background: linear-gradient(to right, #1a2a6c, #fdbb2d); margin: 10px auto 0; border-radius: 2px; } .form-group { margin-bottom: 20px; } label { display: block; margin-bottom: 8px; font-weight: 600; color: #2c3e50; } input { width: 100%; padding: 14px; border: 2px solid #ddd; border-radius: 8px; font-size: 16px; transition: border-color 0.3s; } input:focus { border-color: #1a2a6c; outline: none; box-shadow: 0 0 0 3px rgba(26, 42, 108, 0.2); } .remember-me { display: flex; align-items: center; margin: 20px 0; } .remember-me input { width: auto; margin-right: 10px; } .btn { display: block; width: 100%; padding: 14px; background: linear-gradient(to right, #1a2a6c, #b21f1f); color: white; border: none; border-radius: 8px; font-size: 16px; font-weight: 600; cursor: pointer; transition: all 0.3s ease; } .btn:hover { background: linear-gradient(to right, #23317e, #c42c2c); transform: translateY(-2px); box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2); } .explanation { background: rgba(255, 255, 255, 0.95); border-radius: 16px; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3); width: 600px; padding: 30px; color: #2c3e50; } .explanation h2 { font-size: 1.8rem; margin-bottom: 20px; position: relative; padding-bottom: 10px; } .explanation h2:after { content: ""; display: block; width: 60px; height: 4px; background: linear-gradient(to right, #1a2a6c, #fdbb2d); position: absolute; bottom: 0; left: 0; border-radius: 2px; } .methods { margin-top: 25px; } .method { background: #f8f9fa; padding: 20px; border-radius: 10px; margin-bottom: 15px; border-left: 4px solid #1a2a6c; } .method h3 { margin-bottom: 10px; color: #1a2a6c; } .method p { line-height: 1.6; } .code-block { background: #2c3e50; color: #f8f9fa; padding: 15px; border-radius: 8px; font-family: monospace; font-size: 15px; margin: 15px 0; overflow-x: auto; } .note { background: #fff8e1; padding: 15px; border-radius: 8px; border-left: 4px solid #ffc107; margin: 20px 0; } .note strong { color: #d39e00; } footer { margin-top: 40px; color: white; text-align: center; padding: 20px; font-size: 0.9rem; text-shadow: 0 1px 2px rgba(0,0,0,0.3); } @media (max-width: 768px) { .content { flex-direction: column; align-items: center; } .login-container, .explanation { width: 100%; max-width: 500px; } } </style> </head> <body> <div id="app"> <div class="container"> <header> <h1>禁用浏览器记住密码功能</h1> <p>在某些场景下,出于安全考虑,您可能希望禁用浏览器的自动填充和记住密码功能。本示例展示了在Vue应用中实现此功能的几种有效方法。</p> </header> <div class="content"> <div class="login-container"> <h2 class="login-title">用户登录</h2> <form @submit.prevent="login"> <div class="form-group"> <label for="username">用户名:</label> <input type="text" id="username" v-model="username" autocomplete="off" placeholder="请输入用户名" required > </div> <div class="form-group"> <label for="password">密码:</label> <input type="password" id="password" v-model="password" autocomplete="new-password" placeholder="请输入密码" required > </div> <div class="remember-me"> <input type="checkbox" id="remember" v-model="rememberMe" > <label for="remember">记住我</label> </div> <button type="submit" class="btn">登录</button> </form> <div class="note"> <strong>注意:</strong> 在此表单中,我们通过设置 autocomplete="off" (用户名) 和 autocomplete="new-password" (密码) 来禁用浏览器的记住密码功能。 </div> </div> <div class="explanation"> <h2>实现方法与说明</h2> <p>禁用浏览器记住密码功能主要依靠HTML的<code>autocomplete</code>属性。以下是几种有效方法:</p> <div class="methods"> <div class="method"> <h3>1. 使用 autocomplete="off"</h3> <p>在输入字段上设置此属性可以完全禁用自动填充功能:</p> <div class="code-block"> <input type="text" autocomplete="off"> </div> </div> <div class="method"> <h3>2. 使用 autocomplete="new-password"</h3> <p>对于密码字段,使用此属性告诉浏览器生成一个新密码,而不是记住现有密码:</p> <div class="code-block"> <input type="password" autocomplete="new-password"> </div> </div> <div class="method"> <h3>3. 虚拟输入框技巧</h3> <p>对于某些浏览器,可以添加隐藏的虚拟输入框来"欺骗"浏览器:</p> <div class="code-block"> <!-- 在真实输入框之前添加隐藏的虚拟输入框 --><br> <input type="text" style="display:none"><br> <input type="password" style="display:none"><br> <br> <!-- 真实的用户名输入框 --><br> <input type="text" v-model="username" autocomplete="off"><br> <br> <!-- 真实的密码输入框 --><br> <input type="password" v-model="password" autocomplete="new-password"> </div> </div> </div> <div class="note"> <strong>重要提示:</strong> <ul> <li>不同浏览器对 autocomplete 属性的支持程度不同</li> <li>现代浏览器通常更严格遵守这些属性</li> <li>对于密码字段,优先使用 autocomplete="new-password"</li> <li>结合多种方法可以提高兼容性</li> </ul> </div> </div> </div> <footer> <p>Vue 禁用记住密码功能示例 | 使用 autocomplete 属性控制浏览器行为</p> </footer> </div> </div> <script> const { createApp, ref } = Vue; createApp({ setup() { const username = ref(''); const password = ref(''); const rememberMe = ref(false); const login = () => { // 模拟登录操作 alert(`登录中...\n用户名: ${username.value}\n记住我: ${rememberMe.value ? '是' : '否'}`); // 在实际应用中,这里会有API调用 console.log('登录信息:', { username: username.value, password: password.value, rememberMe: rememberMe.value }); }; return { username, password, rememberMe, login }; } }).mount('#app'); </script> </body> </html> ``` ## 实现原理 禁用浏览器记住密码功能的核心方法是使用HTML的`autocomplete`属性: 1. **autocomplete="off"**: - 完全禁用输入框的自动填充功能 - 适用于用户名、邮箱等普通输入字段 2. **autocomplete="new-password"**: - 特别针对密码字段设计 - 告诉浏览器为当前输入框生成一个新密码 - 阻止浏览器保存当前输入的密码 3. **虚拟输入框技巧**: - 在真实输入框前添加隐藏的`<input>`元素 - "欺骗"浏览器使其认为已经填充了表单 - 提高在旧版浏览器中的兼容性 ## 使用说明 1. 对于用户名输入框,使用 `autocomplete="off"` 2. 对于密码输入框,使用 `autocomplete="new-password"` 3. 对于兼容性要求高的场景,可结合虚拟输入框技巧 这个实现方案适用于大多数现代浏览器,包括Chrome、Firefox、Safari和Edge的最新版本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值