編輯:Android 開發實例
幾乎每一款需要使用者帳號的 App,都得先問一句「你是誰」。登入畫面看似簡單,卻同時牽涉到版面配置、輸入型別、字串比對、錯誤提示,以及最容易被忽略的一環:暴力嘗試的防堵。
本章從零做出一個登入界面,並加上「連續失敗三次就鎖住登入鈕」的安全機制。
使用者名稱用一般文字輸入即可;密碼欄則必須把 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" />
按鈕的文字放在 string.xml 裡,點擊事件則透過 onClick 屬性指向 Java 端的同名方法:
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="login" android:text="@string/Login" />
在 Java 端用 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{ // 帳密錯誤 } }
真實環境的密碼驗證一定放在伺服器端,但「限制嘗試次數」這個觀念值得先在小專案裡練習。做法是用一個計數變數,每失敗一次就遞減,歸零時停用登入鈕:
int counter = 3; counter--; if(counter == 0){ // 停用按鈕、關閉應用程式等 }
| 步驟 | 要做的事 |
|---|---|
| 1 | 用 Android Studio 建立專案 LoginScreen,Target SDK 選新版 SDK。 |
| 2 | 修改 MainActivity.java,加入帳密比對與計數邏輯。 |
| 3 | 在 activity_main.xml 排出兩個輸入框、登入鈕與剩餘次數標籤。 |
| 4 | 在 res/values/string.xml 補上字串。 |
| 5 | 執行並在裝置上驗證。 |
MainActivity.java:
package com.example.loginscreen; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; 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); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
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/textV1" android:text="@string/hello_world" android:layout_centerHorizontal="true" /> <TextView android:id="@+id/textV2" android:text="@string/username" android:layout_below="@id/textV1" /> <EditText android:id="@+id/editText1" android:layout_below="@id/textV1" android:layout_toRightOf="@id/textV2"><requestFocus /></EditText> <TextView android:id="@+id/textV3" android:text="@string/password" android:layout_below="@id/textV2" /> <EditText android:id="@+id/editText2" android:inputType="textPassword" android:layout_alignBaseline="@id/textV3" android:layout_toRightOf="@id/textV3" /> <Button android:id="@+id/button1" android:text="@string/Login" android:onClick="login" android:layout_below="@id/editText2" android:layout_centerHorizontal="true" /> <TextView android:id="@+id/textV4" android:text="@string/attempts" android:layout_below="@id/textV3" /> <TextView android:id="@+id/textV5" android:layout_alignTop="@id/textV4" android:layout_alignRight="@id/textV1" /> </RelativeLayout>
res/values/string.xml:
<?xml version="1.0" encoding="utf-8"?> <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>
AndroidManifest.xml:
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.yiibai.loginscreen"> <application android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
裝到 AVD 或真機啟動,會看到乾淨的登入畫面:
輸入使用者名稱 admin、密碼填錯的內容後按下登入,App 會提示帳密錯誤,並把剩餘次數扣一:
連續失敗三次後,剩餘次數歸零,登入鈕變灰無法再點:
重新開啟 App 讓計數器重置,這次改用正確的帳密 admin / admin,就能順利登入:
要特別注意的是,範例中的計數器只是一個記憶體變數,App 一重啟就會歸零,因此它只能示範觀念,擋不住真正的暴力破解。正式服務應該把失敗次數與鎖定時間記錄在伺服器端,並搭配 HTTPS 傳輸與加密雜湊後的密碼比對。
TableLayout 常用屬性:collapseColumns、shrinkColumns、stretchColumns
TableLayout 是 Android 中常用的表格版面。它透過列與欄的方式排列控制項,並提供多個欄位調整屬性,例如隱藏、延伸與填滿。理解這些屬性能有效解決複雜對齊問
Android Service 完全解析:生命週期與 start/bind 的差別
前面談過 Activity 與 Intent,這次來看 Service。一句話概括:如果把 Activity 比喻成前台程式,Service 就是背景程式,整個生命週期都
在源碼層面獲取當前調用 API 的應用名稱
做 ROM 定製或者修改系統源碼時,經常需要知道「現在是哪個應用在調用我」。比如攝像頭被打開時,想記錄下來是哪個 App 喚醒的。這個信息在 framework 層是可以
Android 入門:把 HelloWorld 專案的每個細節看明白
每一本 Android 書的第一章幾乎都是 HelloWorld。但多數人跑完就跳過了,其實這個看似空蕩蕩的專案裡,藏著整個 Android 開發架構的縮影。把目錄結構與