編輯: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) {
// 停用按鈕、關閉應用程式等
}
| 步驟 | 做法 |
|---|---|
| 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 並啟動,會看到這樣的登入畫面:
輸入帳號 admin、密碼故意打錯(例如 Nimda),按下登入後會看到錯誤提示,剩餘次數同步遞減:
同樣的操作再做一次,次數歸零,登入按鈕變灰、無法再點:
重新開啟 App 讓計數器重置,這次輸入正確的帳號 admin 與密碼 admin,便會成功登入:
要變成真正安全的登入,還少了什麼:計數器存在記憶體裡,App 一重啟就歸零,鎖定等於沒做;正式環境應把嘗試次數與鎖定時間放在伺服器端,並搭配 SharedPreferences 做為本地輔助。送出請求後應立刻停用按鈕並顯示進度指示,避免使用者連點造成重複請求;帳密欄位建議加上 android:maxLines="1" 與 imeOptions="actionNext",讓鍵盤能順手跳到下一欄。若要在裝置失去連線時仍維持基本可用性,也別忘了處理離線狀態的提示。
Android 本地化入門:讓 App 跟著語系切換文字
Android App 常需要在不同語言環境下顯示不同文字。Android 的資源系統會依裝置語言自動選擇對應的 values 目錄,因此只要準備好各語言的 string
Android 登入功能範例:輸入欄位設計與三次失敗後停用按鈕
需要帳號的 App,幾乎都少不了一個登入頁。它看似只是兩個輸入框加一顆按鈕,實際上卻涵蓋了幾個不容忽略的細節:密碼不能被旁人看見、錯誤時要給明確回饋,以及不該讓人無限次嘗
用 MediaPlayer 實作 Android 音樂播放器(含進度條與 ±5 秒跳轉)
要在 App 裡播放一段音樂,Android 給了你不少選擇,但最常被拿出來用的仍是 MediaPlayer。它把底層的播放服務包成一個好操作的物件,不論音樂是打包在 A
Android JSON 網路資料解析:天氣查詢完整思路
在 Android 中,JSON 常用於 API 回傳格式。開發者可以透過 JSONObject 讀取物件欄位,再用 getString()、getInt() 等方法取出