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

Android 登入畫面實作:密碼遮罩、錯誤提示與三次鎖定

編輯:Android 開發實例

幾乎每個需要使用者帳號的 App,都得面對同一道關卡:登入畫面。它看似簡單,卻同時牽涉到輸入欄位的型別設定、按鈕事件綁定、錯誤訊息回饋,以及最容易被忽略的一環——防止暴力嘗試的鎖定機制。

這一章就從零開始,做出一個「三次輸入錯誤就禁用登入鈕」的登入界面。

第一步:兩個輸入欄位

使用者名稱用一般文字輸入即可,密碼欄位的關鍵在於 android: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" />

第二步:登入按鈕

加上要顯示的文字,並用 android:onClick 指定按下後要呼叫的方法名稱,接著在 Java 裡實作同名方法即可:

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

第三步:比對帳號密碼

在 login() 裡用 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 {
        // 帳密錯誤
    }
}

要提醒的是,把帳密寫死在程式碼裡只能當教學用途。真實專案請把驗證交給伺服器端 API,並全程走 HTTPS,密碼更不該以明文形式儲存在任何地方。

第四步:失敗次數鎖定

最後一塊拼圖是安全機制。宣告一個計數變數,每失敗一次就遞減,歸零時直接停用登入按鈕:

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

完整範例:LoginScreen

步驟做法
1用 Android Studio 建立專案,命名為 LoginScreen,Target SDK 與 Compile With 選最新版本。
2修改 src/MainActivity.java,加入登入邏輯。
3修改 res/layout/activity_main.xml,排入標題、兩個輸入框、按鈕與剩餘次數。
4修改 res/values/strings.xml,補上所有字串。
5執行 App,在模擬器或實機上驗證三種情境。

MainActivity.java:

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);
            }
        }
    }
}

activity_main.xml(密碼欄位的 inputType="textPassword" 與按鈕的 onClick="login" 是兩個關鍵屬性):

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/hello_world"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView android:id="@+id/textValue2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textValue1"
        android:layout_marginTop="50dp"
        android:text="@string/username" />

    <EditText android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textValue2"
        android:layout_toRightOf="@+id/textValue2"
        android:ems="10">
        <requestFocus />
    </EditText>

    <TextView android:id="@+id/textValue3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textValue2"
        android:layout_marginTop="38dp"
        android:text="@string/password" />

    <EditText android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textValue3"
        android:layout_alignLeft="@+id/editText1"
        android:ems="10"
        android:inputType="textPassword" />

    <Button android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="94dp"
        android:onClick="login"
        android:text="@string/Login" />

    <TextView android:id="@+id/textValue4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textValue3"
        android:layout_marginTop="48dp"
        android:text="@string/attempts" />

    <TextView android:id="@+id/textValue5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textValue1"
        android:layout_alignTop="@+id/textValue4"
        android:text="TextView" />

</RelativeLayout>

strings.xml:

<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>

把 App 裝到模擬器上啟動,會看到這樣的登入畫面:

LoginScreen 登入畫面

在欄位中輸入資料後按下登入鈕。這裡故意把密碼打錯(例如帳號 admin、密碼 Nimda),會看到「Wrong Credentials」的提示,剩餘次數也同步遞減:

帳密錯誤的提示畫面

再錯一次,剩餘次數歸零,登入按鈕變成不可點擊的灰色狀態:

三次失敗後登入鈕被禁用

重新開啟 App 讓計數器重置,這次輸入正確的帳號 admin 與密碼 admin,即可成功登入:

登入成功的畫面

從範例到 production 還缺什麼:把 counter 存在記憶體裡,App 一重啟就歸零,等於鎖定機制形同虛設;正式環境應該把嘗試次數與鎖定時間寫入伺服器端,並搭配 SharedPreferences 或加密儲存做為輔助。此外,登入按鈕應該在送出請求後立即停用並顯示進度指示,避免使用者連點造成重複請求;帳密欄位也建議加上 android:maxLines="1" 與適當的 imeOptions,讓鍵盤的「下一步」能直接跳到密碼欄。

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