国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android實現(xiàn)帶有記住密碼功能的登陸界面

2019-12-12 06:21:53
字體:
供稿:網(wǎng)友

本文實例為大家分享了Android帶有記住密碼功能的登陸界面實現(xiàn)代碼,供大家參考,具體內(nèi)容如下

1、設(shè)計思路

主要采用SharedPreferences來保存用戶數(shù)據(jù),本Demo沒有經(jīng)過加密,所有一旦Android系統(tǒng)被ROOT的話,其他用戶就可以查看用戶的私有目錄,密碼文件就很不安全。所以真正應(yīng)用在軟件上面的,一定要經(jīng)過加密才保存,可以選擇MD5加密。

SharedPreferences介紹可以參看這篇博文://m.survivalescaperooms.com/article/84859.htm

TextWatcher的介紹可以參看這篇博文://m.survivalescaperooms.com/article/84865.htm 

2、功能介紹

默認(rèn)勾選“記住密碼”復(fù)選框,點擊“登陸”按鈕,一旦成功登陸,就保存用戶名和密碼到SharedPreferences文件中。

用戶名輸入時,通過TextWatcher不斷去讀取用戶數(shù)據(jù),自動提示相應(yīng)的“用戶名”,選擇了用戶名之后,就會讀取SharedPreferences的文件,然后自動完成密碼的輸入。 

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個字母就開始自動提示  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()返回的是有多少個鍵值對  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(), ""));// 自動輸入密碼   }  });   // 登陸  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, "密碼錯誤,請重新輸入",   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)到另一個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="簡單登陸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> 

以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)Android軟件編程有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 伊川县| 报价| 吴旗县| 揭西县| 肥城市| 伊金霍洛旗| 电白县| 三河市| 石棉县| 鹤峰县| 五原县| 资阳市| 河南省| 鄂州市| 嘉禾县| 泗水县| 青龙| 邵东县| 兴化市| 大田县| 固阳县| 邢台县| 佛山市| 广汉市| 巩留县| 德安县| 鞍山市| 孟州市| 都安| 龙陵县| 镇坪县| 三江| 汉沽区| 屏东县| 龙门县| 榆林市| 基隆市| 根河市| 平安县| 海城市| 平安县|