編輯: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) {
// 停用按鈕、關閉應用程式等
}
| 步驟 | 做法 |
|---|---|
| 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,按下登入鈕。這次嘗試會失敗,畫面跳出錯誤提示,剩餘次數同步減少:
再錯一次,剩餘次數變成 0,登入按鈕同時被停用、無法再點擊:
重新開啟 App(計數器重置),輸入正確的帳號 admin 與密碼 admin 後按下登入,即會成功進入:
要成為真正可靠的登入流程,還需要補上:計數器只存在記憶體,App 一重啟就歸零,鎖定機制形同虛設——正式環境應把嘗試次數與鎖定時間放在伺服器端,並以 SharedPreferences 或加密儲存做為本地輔助。送出請求後應立即停用按鈕並顯示進度指示,避免使用者連點送出重複請求;帳密欄位建議加上 android:maxLines="1" 與 imeOptions,讓鍵盤的「下一步」能順勢跳到密碼欄。最後,別忘了在無網路時給出清楚的離線提示,而不是讓使用者盯著一個沒有回應的畫面。
開啟 ListView 側邊快速滾動滑塊:一行屬性搞定 fastScroll
不少開發者不知道 ListView 右側那個快速滾動滑塊(fast scroll thumb)該怎麼啟用,其實只要一行設定。 用 XML 布局的話,在 ListView
Android 載入 Spinner:實作不確定模式的進度提示
在 Android 裡要讓使用者知道「系統正在忙」,最直接的方式就是拉一個進度條出來。進度條大致分成兩種形態:一種是進度明碼標示的 Loading Bar,另一種則是只轉
Android 載入 Spinner 實作流程:從 XML 到 Java 的完整示範
當 App 在背後做事,畫面上一定要給使用者一點回饋,否則再快的操作也會被當成當機。Android 的進度條有兩種典型形態:一種是能看到百分比的 Loading Bar,
Facebook shimmer 閃光效果庫 Android 使用指南
關於 shimmerShimmer 是 Facebook 開源的一套 Java 函式庫,能讓 Android 上任何 View 元件披上一層流動的高光效果。常見用途是「骨