Android實(shí)現(xiàn)帶有記住密碼功能的登陸界面
本文實(shí)例為大家分享了Android帶有記住密碼功能的登陸界面實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
1、設(shè)計(jì)思路
主要采用SharedPreferences來(lái)保存用戶數(shù)據(jù),本Demo沒(méi)有經(jīng)過(guò)加密,所有一旦Android系統(tǒng)被ROOT的話,其他用戶就可以查看用戶的私有目錄,密碼文件就很不安全。所以真正應(yīng)用在軟件上面的,一定要經(jīng)過(guò)加密才保存,可以選擇MD5加密。
SharedPreferences介紹可以參看這篇博文
TextWatcher的介紹可以參看這篇博文
2、功能介紹
默認(rèn)勾選“記住密碼”復(fù)選框,點(diǎn)擊“登陸”按鈕,一旦成功登陸,就保存用戶名和密碼到SharedPreferences文件中。
用戶名輸入時(shí),通過(guò)TextWatcher不斷去讀取用戶數(shù)據(jù),自動(dòng)提示相應(yīng)的“用戶名”,選擇了用戶名之后,就會(huì)讀取SharedPreferences的文件,然后自動(dòng)完成密碼的輸入。
3、效果圖
4、代碼:詳細(xì)都在注釋里面了
/*author: conowen * date: 2012.4.2 * */ package com.conowen.remeberPwd; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.text.Editable; import android.text.InputType; import android.text.TextWatcher; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class RemeberPwdActivity extends Activity { AutoCompleteTextView cardNumAuto; EditText passwordET; Button logBT; CheckBox savePasswordCB; SharedPreferences sp; String cardNumStr; String passwordStr; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); cardNumAuto = (AutoCompleteTextView) findViewById(R.id.cardNumAuto); passwordET = (EditText) findViewById(R.id.passwordET); logBT = (Button) findViewById(R.id.logBT); sp = this.getSharedPreferences("passwordFile", MODE_PRIVATE); savePasswordCB = (CheckBox) findViewById(R.id.savePasswordCB); savePasswordCB.setChecked(true);// 默認(rèn)為記住密碼 cardNumAuto.setThreshold(1);// 輸入1個(gè)字母就開(kāi)始自動(dòng)提示 passwordET.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); // 隱藏密碼為InputType.TYPE_TEXT_VARIATION_PASSWORD,也就是0x81 // 顯示密碼為InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD,也就是0x91 cardNumAuto.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub String[] allUserName = new String[sp.getAll().size()];// sp.getAll().size()返回的是有多少個(gè)鍵值對(duì) allUserName = sp.getAll().keySet().toArray(new String[0]); // sp.getAll()返回一張hash map // keySet()得到的是a set of the keys. // hash map是由key-value組成的 ArrayAdapter<String> adapter = new ArrayAdapter<String>( RemeberPwdActivity.this, android.R.layout.simple_dropdown_item_1line, allUserName); cardNumAuto.setAdapter(adapter);// 設(shè)置數(shù)據(jù)適配器 } @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 passwordET.setText(sp.getString(cardNumAuto.getText() .toString(), ""));// 自動(dòng)輸入密碼 } }); // 登陸 logBT.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub cardNumStr = cardNumAuto.getText().toString(); passwordStr = passwordET.getText().toString(); if (!((cardNumStr.equals("test")) && (passwordStr .equals("test")))) { Toast.makeText(RemeberPwdActivity.this, "密碼錯(cuò)誤,請(qǐng)重新輸入", Toast.LENGTH_SHORT).show(); } else { if (savePasswordCB.isChecked()) {// 登陸成功才保存密碼 sp.edit().putString(cardNumStr, passwordStr).commit(); } Toast.makeText(RemeberPwdActivity.this, "登陸成功,正在獲取用戶數(shù)據(jù)……", Toast.LENGTH_SHORT).show(); // 跳轉(zhuǎn)到另一個(gè)Activity // do something } } }); } }
布局文件:main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="簡(jiǎn)單登陸DEMO" android:textSize="25px" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" > <LinearLayout android:layout_width="250dip" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="15dp" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <LinearLayout android:layout_width="fill_parent" android:layout_height="80px" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="40px" android:orientation="horizontal" > <TextView android:id="@+id/tv_account" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:text="用 戶 名:" android:textSize="15px" /> <AutoCompleteTextView android:id="@+id/cardNumAuto" android:layout_width="fill_parent" android:layout_height="40px" > </AutoCompleteTextView> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="40px" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:text="用戶密碼:" android:textSize="15px" /> <EditText android:id="@+id/passwordET" android:layout_width="fill_parent" android:layout_height="40px" > </EditText> </LinearLayout> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <CheckBox android:id="@+id/savePasswordCB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="記住密碼" > </CheckBox> <Button android:id="@+id/logBT" android:layout_width="100px" android:layout_height="wrap_content" android:layout_marginLeft="40dp" android:layout_marginRight="10dp" android:text="登錄" > </Button> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout>
SharedPreferences文件,在/data/data/包名/shared_prefs文件夾下面
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <string name="test">test</string> <string name="test2">test</string> <string name="test1">test</string> </map>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)Android軟件編程有所幫助。
- Android實(shí)現(xiàn)記住密碼功能
- Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄
- Android開(kāi)發(fā)筆記SQLite優(yōu)化記住密碼功能
- Android實(shí)現(xiàn)用戶登錄記住密碼功能
- Android sharedPreferences實(shí)現(xiàn)記住密碼功能
- Android 使用SharedPreferrences儲(chǔ)存密碼登錄界面記住密碼功能
- Android實(shí)現(xiàn)登錄界面記住密碼的存儲(chǔ)
- Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄界面
- Android通過(guò)記住密碼功能學(xué)習(xí)數(shù)據(jù)存儲(chǔ)類SharedPreferences詳解及實(shí)例
- Android實(shí)現(xiàn)登陸界面的記住密碼功能
相關(guān)文章
教你如何搭建android源代碼repo倉(cāng)庫(kù)
這篇文章主要介紹了如何搭建android源代碼repo倉(cāng)庫(kù),如果你的開(kāi)發(fā)是基于AOSP源碼來(lái)建倉(cāng),那么搭建repo服務(wù)器和部署自己的repo倉(cāng)庫(kù)就是非常必要的工作了,本文給大家詳細(xì)介紹搭建過(guò)程,感興趣的朋友一起看看吧2022-07-07Android圖片添加水印圖片并把圖片保存到文件存儲(chǔ)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android圖片添加水印圖片并把圖片保存到文件存儲(chǔ)的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06Android開(kāi)發(fā)實(shí)現(xiàn)帶有反彈效果仿IOS反彈scrollview教程詳解
本文給大家分享android開(kāi)發(fā)實(shí)現(xiàn)帶有反彈效果,模仿ios反彈scrollview詳細(xì)教程,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-09-09Android ListView UI組件使用說(shuō)明
這篇文章主要介紹了Android ListView UI組件使用說(shuō)明,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Android 簡(jiǎn)單的照相機(jī)程序的實(shí)例代碼
終于經(jīng)過(guò)多次找錯(cuò),修改把一個(gè)簡(jiǎn)單的照相機(jī)程序完成了,照相類代碼如下:2013-05-05Android實(shí)現(xiàn)帶指示點(diǎn)的自動(dòng)輪播無(wú)限循環(huán)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶指示點(diǎn)的自動(dòng)輪播無(wú)限循環(huán)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android PC端用ADB抓取指定應(yīng)用日志實(shí)現(xiàn)步驟
這篇文章主要介紹了Android PC端用ADB抓取指定應(yīng)用日志實(shí)現(xiàn)步驟,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04Android中ImageView無(wú)法居中的問(wèn)題解決方法
做UI布局,尤其是遇到比較復(fù)雜的多重LinearLayout嵌套,常常會(huì)被一些比較小的問(wèn)題困擾上半天,比如今天在使用ImageView的時(shí)候,想讓其居中顯示,可是無(wú)論怎樣設(shè)置layout_gravity屬性,都無(wú)法達(dá)到效果2013-06-06android監(jiān)聽(tīng)軟鍵盤(pán)的彈出與隱藏的示例代碼
本篇文章主要介紹了android監(jiān)聽(tīng)軟鍵盤(pán)的彈出與隱藏的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02