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

Android 登入功能實作全解析:從 textPassword 到三次失敗鎖定

編輯:Android 開發實例

登入畫面是大多數 App 的第一道門。它看似只是「兩個輸入框加一顆按鈕」,但真正做過就會發現,密碼欄的遮蔽設定、按鈕的事件綁定、錯誤態度的回饋、以及防止暴力嘗試的機制,每一項都得處理。

以下依序拆解這四個部分,最後用一個完整範例把它們串起來。

一、帳號與密碼輸入欄

帳號欄用一般 EditText 就好。密碼欄的關鍵是 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 方法:

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

三、比對輸入內容

取出字串後用 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,按下登入鈕。這次嘗試會失敗,畫面跳出錯誤提示,剩餘次數同步減少:

帳密錯誤的畫面

再錯一次,剩餘次數變成 0,登入按鈕同時被停用、無法再點擊:

三次失敗後登入鈕被禁用

重新開啟 App(計數器重置),輸入正確的帳號 admin 與密碼 admin 後按下登入,即會成功進入:

登入成功後的畫面

要成為真正可靠的登入流程,還需要補上:計數器只存在記憶體,App 一重啟就歸零,鎖定機制形同虛設——正式環境應把嘗試次數與鎖定時間放在伺服器端,並以 SharedPreferences 或加密儲存做為本地輔助。送出請求後應立即停用按鈕並顯示進度指示,避免使用者連點送出重複請求;帳密欄位建議加上 android:maxLines="1" 與 imeOptions,讓鍵盤的「下一步」能順勢跳到密碼欄。最後,別忘了在無網路時給出清楚的離線提示,而不是讓使用者盯著一個沒有回應的畫面。

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