使用sharedPreferences实现密码登录和注册用户名重复检查
来源:互联网 发布:国家统计局2016年数据 编辑:程序博客网 时间:2024/05/07 21:31
本文通过sharedPreferences实现密码登录以及注册用户是否重复的检查。
无法上传截图,直接上代码。实际项目中涉及用户账户密码的基本都是用数据库保存,而且经过加密(MD5 等)。
LoginActivity.java代码
package com.example.test;import java.security.MessageDigest;import java.util.ArrayList;import java.util.HashMap;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class LoginActivity extends Activity { private int count; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); Button login = (Button) findViewById(R.id.login); Button register = (Button) findViewById(R.id.register); final EditText username = (EditText) findViewById(R.id.username); final EditText password = (EditText) findViewById(R.id.password); final TextView tips = (TextView) findViewById(R.id.tips); //int[] num={1,2}; //final HashMap<String,String> hashMap=new HashMap<String,String>(); count = MySharedPreference.getIntValue(LoginActivity.this, "count"); tips.setText("当前共有" + count + "人注册"); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String strUsername = username.getText().toString(); String strPassword = password.getText().toString(); String defaultPass = MySharedPreference.getStringValue(LoginActivity.this, strUsername); if (defaultPass.equals(strPassword)) { //匹配成功登录跳转 Intent intent = new Intent(LoginActivity.this, FaceActivity.class); startActivity(intent); finish(); } else { showMessage("error"); password.setText(""); } } }); register.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String strUsername = username.getText().toString(); String strPassword = password.getText().toString(); boolean isNameRepeat = false; Log.i("czktest", "strUsername=" + strUsername + ",strPassword=" + strPassword); if (!strUsername.equals("") && !strPassword.equals("")) { //hashMap.put("one", strUsername); String[] names = new String[10];//最多保存10个用户 if (count > 0) { for (int i = 0; i < count; i++) {//检查是否有用户名重复 names[i] = String.valueOf(i); String name = MySharedPreference.getStringValue(LoginActivity.this, names[i]); if (name.equals(strUsername)) { showMessage("exist"); isNameRepeat = true; break; } } } if (!isNameRepeat) { //用户名不重复执行保存(用户名--密码) MySharedPreference.register(LoginActivity.this, strUsername, strPassword); //用户名不重复执行保存(数组下标--用户名) names[count] = String.valueOf(count); MySharedPreference.saveNames(LoginActivity.this, names[count], strUsername); //用户名不重复执行保存(注册用户数) count = count + 1; MySharedPreference.saveCount(LoginActivity.this, "count", count); tips.setText("当前共有" + count + "人注册"); showMessage("success"); } } else { showMessage(""); } } }); } public void showMessage(String str) { if (str.equals("error")) { Toast.makeText(LoginActivity.this, "用户名和密码不匹配", Toast.LENGTH_SHORT).show(); } else if (str.equals("success")) { Toast.makeText(LoginActivity.this, "注册成功", Toast.LENGTH_SHORT).show(); } else if (str.equals("exist")) { Toast.makeText(LoginActivity.this, "当前用户已被注册", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(LoginActivity.this, "用户名或密码为空", Toast.LENGTH_SHORT).show(); } }}MySharedPreference.java代码
package com.example.test;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.preference.PreferenceManager;public class MySharedPreference { private static SharedPreferences preContext = null; private static SharedPreferences getPreference(Context cx) { return PreferenceManager.getDefaultSharedPreferences(cx); } public static void register(Context cx, String username, String password) { setValue(cx, username, password); } public static void saveNames(Context cx, String username, String password) { setValue(cx, username, password); } public static String getStringValue(Context cx, String key) { if (preContext == null) { preContext = getPreference(cx); } return preContext.getString(key, "error"); } private static void setValue(Context cx, String key, String val) { if (preContext == null) { preContext = getPreference(cx); } Editor ed = preContext.edit(); ed.putString(key, val); boolean ret = ed.commit(); } public static void saveCount(Context cx, String key, int count) { setValue(cx, key, count); } public static int getIntValue(Context cx, String key) { if (preContext == null) { preContext = getPreference(cx); } return preContext.getInt(key, 0); } private static void setValue(Context cx, String key, int val) { if (preContext == null) { preContext = getPreference(cx); } Editor ed = preContext.edit(); ed.putInt(key, val); boolean ret = ed.commit(); }}login.xml布局代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_marginTop="16dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">" <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="用户名:" android:layout_weight="3" android:gravity="right"/> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1"/>" </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="密 码:" android:layout_weight="3" android:gravity="right"/> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:layout_weight="1"/> </LinearLayout> <Button android:id="@+id/login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" android:textSize="18dp" android:gravity="center"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:textSize="16dp"/> <TextView android:alpha="1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1.0" android:textSize="16dp" android:gravity="center" android:id="@+id/tips" /> <Button android:id="@+id/forgetpass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="忘记密码" android:textSize="16dp" /> </LinearLayout></LinearLayout>
阅读全文
0 0
- 使用sharedPreferences实现密码登录和注册用户名重复检查
- 使用 SharedPreferences 实现记住用户名和密码功能
- 使用SharedPreferences实现简单的记住用户名和密码功能
- 使用SharedPreferences保存用户名和密码
- 使用SharedPreferences实现记住密码和自动登录
- Android自动登录 记住用户名密码实现 SharedPreferences
- Android自动登录 记住用户名密码实现 SharedPreferences
- Android自动登录 记住用户名密码实现 SharedPreferences
- Android自动登录 记住用户名密码实现 SharedPreferences
- 保存用户名和密码(SharedPreferences)
- SharedPreferences 保存用户名和密码
- Android用SharedPreferences保存数据实现注册和登录功能(首次安装默认打开注册,记住密码)
- Android 使用sharedpreferences、ACache缓存用户名和密码
- Android 使用sharedpreferences、ACache缓存用户名和密码
- ubuntu 正确输入 用户名和密码 后 重复登录界面
- PHP实现注册登录,并实现注册时动态检查用户名是否可用
- 使用SharedPreferences实现登录记住密码功能实现
- android SharedPreferences实现用户的注册和保存账号密码
- oc统计数组相同元素的个数,以及数组排序
- 任意进制的数值转换 C++版
- Quartz定时任务corn表达式解析
- 2017年澳洲建筑师协会奖入围作品:办公空间
- 学习笔记
- 使用sharedPreferences实现密码登录和注册用户名重复检查
- NOI 2017 机房酱油记
- 位运算面试题常用技巧
- spring-boot项目启动失败的一种解决办法:自带的tomcat容器切换成jetty容器
- 海量数据文本相似度解决方式SimHash+分词方法+基于内容推荐算法
- Java:基础练习-循环练习
- jquery 给文本框赋值
- Android ADT代码提示问题
- Delphi遍历进程并获取进程路径