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

登入驗證

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();
        counter--;
        attempts.setText(Integer.toString(counter));

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

執行畫面

應用啟動後,會看到登入畫面與剩餘嘗試次數。

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

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

當三次嘗試都用完,登入按鈕會被停用。

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

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