編輯:Android 開發實例
只要 App 需要帳號,就一定會有一塊登入介面:使用者輸入帳號與密碼,按下按鈕,由程式決定放行或退回。Facebook、微博這類服務是這樣,自家開發的企業 App 也是這樣。本章動手做一個登入畫面,並把兩個實務上絕對會遇到的細節處理乾淨——密碼不能被旁人看光,連續輸錯時入口要懂得關起來。
版面需要的元件不多:兩個 EditText 分別收帳號與密碼,加上一個觸發登入的 Button。關鍵在密碼欄必須加上 android:inputType="textPassword",Android 才會把輸入內容渲染成圓點;少了這行,密碼就以明文攤在螢幕上。
<EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" />
接著是登入按鈕。這裡用 android:onClick="login" 把點擊事件直接綁到 Activity 中名為 login 的方法,省下撰寫監聽器的工:
<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() 比對。範例把帳號與密碼都寫死成 admin 方便測試;真實專案請改為呼叫後端驗證 API,不要把憑證硬編進程式碼。
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")){ //correcct password }else{ //wrong password }
只有比對還不夠。若使用者能無限次嘗試,暴力猜密碼的成本就太低。通用做法是配置一個計數器,每輸錯一次遞減,歸零時停用登入按鈕(正式環境通常還會疊加鎖定期或改採更嚴謹的驗證流程)。
int counter = 3; counter--; if(counter==0){ //disble the button, close the application e.t.c }
以下範例做出一個只給三次機會的登入 App,適合在實機或模擬器上直接驗證。
| 步驟 | 說明 |
|---|---|
| 1 | 用 Android Studio 建立專案,命名為 LoginScreen,Target SDK 與 Compile With 盡量對齊較新的 API 等級。 |
| 2 | 修改 src/MainActivity.java,補上登入邏輯。 |
| 3 | 修改 res/layout/activity_main.xml,加入對應元件。 |
| 4 | 修改 res/values/string.xml,補齊字串資源。 |
| 5 | 執行 App,在 Android 裝置上安裝並驗證結果。 |
主活動 src/com.yiibai.loginscreen/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) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
版面檔 res/layout/activity_main.xml:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="18dp" android:text="@string/hello_world" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:layout_marginTop="50dp" android:text="@string/username" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView2" android:layout_marginLeft="32dp" android:layout_toRightOf="@+id/textView2" android:ems="10" > <requestFocus /> </EditText> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView2" android:layout_below="@+id/textView2" android:layout_marginTop="38dp" android:text="@string/password" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView3" 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/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView3" android:layout_below="@+id/textView3" android:layout_marginLeft="30dp" android:layout_marginTop="48dp" android:text="@string/attempts" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/textView1" android:layout_alignTop="@+id/textView4" android:text="TextView" /> </RelativeLayout>
字串資源 res/values/string.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">LoginScreen</string> <string name="action_settings">Settings</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:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.yiibai.loginscreen" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.yiibai.loginscreen.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>
把 App 裝到 AVD 或實機啟動,設定無誤就會看到這樣的畫面:
在帳號欄輸入 admin,密碼欄故意填 Nimda,按下登入——這是一次刻意失敗的嘗試:
同樣的錯誤再犯一次,剩餘次數會歸零,登入按鈕同時被停用:
重新開啟 App 後計數器重置,這次帳號 admin、密碼 admin,按下登入即可成功通過。
Android 多語言應用:本地化字串與 values-it 資料夾怎麼用
一支上架的 App,很可能同時被台灣、日本、德國的使用者打開。要讓每個人都看得懂,App 就得依裝置的語言環境,換上對應的文字、數字與格式。這件事在 Android 裡稱
Android JSON 解析入門:用 JSONObject 撈出你要的那幾個欄位
JSON(JavaScript Object Notation)自從被提出之後,因為格式輕量、可讀性高,很快就在 Web API 的世界裡取代了 XML 的位置。不管是瀏
Android 接入新浪微博、騰訊微博與人人網的 OAuth 2.0 實作
許多 Android 應用會需要接入社交平台的分享、登入或發文能力。本文以新浪微博、騰訊微博與人人網為例,說明整合時最關鍵的授權流程,以及如何取得後續呼叫 API 所需的
Android MediaPlayer 多媒體播放完全解析
Android 提供了多種控制音訊與影片播放的方式,無論是播放本機檔案還是串流媒體都能勝任,而其中最常被使用的,莫過於 MediaPlayer 這個類別。 Android