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

Android 登入畫面範例:帳號密碼與錯誤次數限制

編輯:Android 開發實例

登入畫面常用來驗證使用者身份。實作時通常需要兩個輸入欄位:使用者名稱與密碼,並加上登入按鈕。為了避免暴力嘗試,還可以限制錯誤次數。

建立輸入欄位

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

登入按鈕

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

驗證帳號密碼

public void login(View view) {
    if (username.getText().toString().equals("admin") &&
        password.getText().toString().equals("admin")) {
        // 登入成功
    } else {
        // 登入失敗
    }
}

限制嘗試次數

int counter = 3;
counter--;
if (counter == 0) {
    login.setEnabled(false);
}

範例會顯示剩餘嘗試次數。當次數歸零,登入按鈕會被停用。

Android Login Application TutZ喎?/kf/ware/vc/

輸入錯誤的帳號或密碼後,會看到錯誤提示與剩餘次數。

當嘗試次數用完,登入按鈕將無法再按。

重新開啟應用並輸入正確帳號密碼後,即可成功登入。

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