Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 登入畫面實作:表單驗證與「三次機會」的失敗鎖定機制

編輯:Android 開發實例

幾乎每一款需要使用者帳號的 App,都得先問一句「你是誰」。登入畫面看似簡單,卻同時牽涉到版面配置、輸入型別、字串比對、錯誤提示,以及最容易被忽略的一環:暴力嘗試的防堵。

本章從零做出一個登入界面,並加上「連續失敗三次就鎖住登入鈕」的安全機制。

第一步:準備兩個輸入框

使用者名稱用一般文字輸入即可;密碼欄則必須把 inputType 設為 textPassword,畫面上才會顯示圓點而不是明文:

 <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" />

第二步:加上登入鈕

按鈕的文字放在 string.xml 裡,點擊事件則透過 onClick 屬性指向 Java 端的同名方法:

 <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="login" android:text="@string/Login" />

第三步:比對帳密

在 Java 端用 getText() 取出輸入內容,toString() 轉成字串,再用 equals() 比對:

 EditText username = (EditText)findViewById(R.id.editText1); EditText password = (EditText)findViewById(R.id.editText2); public void login(View view){ if(username.getText().toString().equals("admin") && password.getText().toString().equals("admin")){ // 帳密正確 } else{ // 帳密錯誤 } }

第四步:限制嘗試次數

真實環境的密碼驗證一定放在伺服器端,但「限制嘗試次數」這個觀念值得先在小專案裡練習。做法是用一個計數變數,每失敗一次就遞減,歸零時停用登入鈕:

 int counter = 3; counter--; if(counter == 0){ // 停用按鈕、關閉應用程式等 }

範例:只有三次機會的登入 App

步驟要做的事
1用 Android Studio 建立專案 LoginScreen,Target SDK 選新版 SDK。
2修改 MainActivity.java,加入帳密比對與計數邏輯。
3在 activity_main.xml 排出兩個輸入框、登入鈕與剩餘次數標籤。
4在 res/values/string.xml 補上字串。
5執行並在裝置上驗證。

MainActivity.java:

 package com.example.loginscreen; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private EditText username=null; private EditText password=null; private TextView attempts; private Button login; int counter = 3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); username = (EditText)findViewById(R.id.editText1); password = (EditText)findViewById(R.id.editText2); attempts = (TextView)findViewById(R.id.textView5); attempts.setText(Integer.toString(counter)); login = (Button)findViewById(R.id.button1); } public void login(View view){ if(username.getText().toString().equals("admin") && password.getText().toString().equals("admin")){ Toast.makeText(getApplicationContext(), "Redirecting...", Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(getApplicationContext(), "Wrong Credentials", Toast.LENGTH_SHORT).show(); attempts.setBackgroundColor(Color.RED); counter--; attempts.setText(Integer.toString(counter)); if(counter==0){ login.setEnabled(false); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }

activity_main.xml(省略純樣式屬性):

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textV1" android:text="@string/hello_world" android:layout_centerHorizontal="true" /> <TextView android:id="@+id/textV2" android:text="@string/username" android:layout_below="@id/textV1" /> <EditText android:id="@+id/editText1" android:layout_below="@id/textV1" android:layout_toRightOf="@id/textV2"><requestFocus /></EditText> <TextView android:id="@+id/textV3" android:text="@string/password" android:layout_below="@id/textV2" /> <EditText android:id="@+id/editText2" android:inputType="textPassword" android:layout_alignBaseline="@id/textV3" android:layout_toRightOf="@id/textV3" /> <Button android:id="@+id/button1" android:text="@string/Login" android:onClick="login" android:layout_below="@id/editText2" android:layout_centerHorizontal="true" /> <TextView android:id="@+id/textV4" android:text="@string/attempts" android:layout_below="@id/textV3" /> <TextView android:id="@+id/textV5" android:layout_alignTop="@id/textV4" android:layout_alignRight="@id/textV1" /> </RelativeLayout>

res/values/string.xml:

 <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">LoginScreen</string> <string name="hello_world">Login Screen</string> <string name="username">Username:</string> <string name="password">Password:</string> <string name="Login">Login</string> <string name="attempts">Attempts Left:</string> </resources>

AndroidManifest.xml:

 <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.yiibai.loginscreen"> <application android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

實際操作驗證

裝到 AVD 或真機啟動,會看到乾淨的登入畫面:

Android Login Application TutZ喎?/kf/ware/vc/vcmlhbA==" data-ke-="" src="/uploadfile/2016/0203/20160203103046962.jpg" />

輸入使用者名稱 admin、密碼填錯的內容後按下登入,App 會提示帳密錯誤,並把剩餘次數扣一:

連續失敗三次後,剩餘次數歸零,登入鈕變灰無法再點:

重新開啟 App 讓計數器重置,這次改用正確的帳密 admin / admin,就能順利登入:

要特別注意的是,範例中的計數器只是一個記憶體變數,App 一重啟就會歸零,因此它只能示範觀念,擋不住真正的暴力破解。正式服務應該把失敗次數與鎖定時間記錄在伺服器端,並搭配 HTTPS 傳輸與加密雜湊後的密碼比對。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved