使用sharedPreferences实现密码登录和注册用户名重复检查
2017-07-19 15:04
453 查看
本文通过sharedPreferences实现密码登录以及注册用户是否重复的检查。
无法上传截图,直接上代码。实际项目中涉及用户账户密码的基本都是用数据库保存,而且经过加密(MD5 等)。
LoginActivity.java代码
无法上传截图,直接上代码。实际项目中涉及用户账户密码的基本都是用数据库保存,而且经过加密(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.th 4000 is, 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>
相关文章推荐
- JavaWeb使用Cookie模拟实现自动登录功能(不需用户名和密码)
- 使用JSP Servlet和Ajax实现简单的注册页面的用户名密码验证
- iOS 开发 -- 使用KeyChain保存用户名、密码并实现自动登录
- Python使用selenium实现网页用户名 密码 验证码自动登录功能
- 使用windows窗体应用程序来实现用户名与密码的登录
- 使用openssh实现免密码登录,免用户名+密码登录
- PHP实现注册登录,并实现注册时动态检查用户名是否可用
- 新建一个登录,找回密码,注册页面的工程,使用控制器实现三个页面的跳转
- 使用Forms Authentication实现用户注册、登录 (一)基础知识
- 使用用户名和密码验证apache登录
- 使用Forms Authentication实现用户注册、登录 (二)用户注册与登录
- 使用Forms Authentication实现用户注册、登录 (一)基础知识
- ubuntu正确输入用户名密码后重复登录界面
- 使用Forms Authentication实现用户注册、登录 (一)基础知识
- 使用Forms Authentication实现用户注册、登录
- 使用Forms Authentication实现用户注册、登录 (一)基础知识
- 使用Forms Authentication实现用户注册、登录
- 简单的流操作实现账户的注册、登录、修改密码
- 使用Forms Authentication实现用户注册、登录 (三)用户实体替换
- 使用ssh公钥实现ssh免密码登录