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

Android 登入頁面 from scratch:輸入欄位、錯誤提示到次數鎖定

編輯: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" />

二、登入按鈕與事件綁定

在 XML 上以 android:onClick 指定方法名,接著在 Activity 中實作同名的 public 方法,並帶一個 View 參數:

<Button android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="login"
    android:text="@string/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 {
        // 帳密錯誤
    }
}

再次強調:寫死帳密只能當教學。真實應用請把驗證交給後端,全程使用 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:

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

    <TextView android:id="@+id/textValue1"
        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 裝到 AVD 並啟動,會看到這樣的登入畫面:

LoginScreen 登入畫面

輸入帳號 admin、密碼故意打錯(例如 Nimda),按下登入後會看到錯誤提示,剩餘次數同步遞減:

帳密錯誤時的畫面

同樣的操作再做一次,次數歸零,登入按鈕變灰、無法再點:

三次失敗後登入鈕被停用

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

登入成功的畫面

要變成真正安全的登入,還少了什麼:計數器存在記憶體裡,App 一重啟就歸零,鎖定等於沒做;正式環境應把嘗試次數與鎖定時間放在伺服器端,並搭配 SharedPreferences 做為本地輔助。送出請求後應立刻停用按鈕並顯示進度指示,避免使用者連點造成重複請求;帳密欄位建議加上 android:maxLines="1" 與 imeOptions="actionNext",讓鍵盤能順手跳到下一欄。若要在裝置失去連線時仍維持基本可用性,也別忘了處理離線狀態的提示。

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