編輯:Android 開發實例
登入畫面幾乎是每個需要帳號服務的應用都會遇到的題目——不論是自家服務,還是透過 Facebook、微博等第三方帳號授權。一個稱職的登入介面除了「能登入」,還必須處理好密碼遮蔽、錯誤提示,以及避免暴力嘗試的防護機制。
本章就從最基礎的三個要素談起。
先用兩個 EditText 取得使用者名稱與密碼。關鍵在於密碼欄必須加上 android:inputType="textPassword",畫面上才會顯示圓點而不是明文:
<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 指定,接著在 Java 裡實作同名方法:
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="login"
android:text="@string/Login" />
在 onClick 方法中用 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 選最新版本。 |
| 2 | 修改 src/MainActivity.java,加入必要程式碼。 |
| 3 | 修改 res/layout/activity_main.xml,加入版面元件。 |
| 4 | 修改 res/values/string.xml,加入字串資源。 |
| 5 | 執行應用程式並驗證結果。 |
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;
}
}
<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>
<?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>
<?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>
把應用程式裝進 AVD 啟動,會看到登入畫面與剩餘次數 3:
輸入使用者名稱 admin、密碼刻意打錯(例如 Nimda),按下登入按鈕,會看到錯誤提示並扣一次:
再錯一次,剩餘次數變成 0,登入按鈕同時被停用:
重新開啟應用程式,這次輸入正確的 admin/admin 並按下登入,就會成功通過:
要提醒的是,這個範例把帳密直接寫在程式碼裡,只用來示範流程。真實專案應該把驗證交給伺服器端 API,並改用 HTTPS 傳輸,避免憑證外洩。
Android 自訂 Toast:讓提示訊息符合 App 的視覺風格
系統預設的 Toast 簡潔好用,一行 makeText 就能搞定。問題是它長得實在太「系統」了,放在一個講究視覺設計的 App 裡總顯得格格不入。好在 Toast 的樣
Android 自訂 View 實作:用 Canvas 畫出可互動的正弦波
正弦波是訊號處理與物理課程的常客,但把它畫在 Android 畫面上,其實是一次很好的 Canvas 與自訂 View 練習。本文從零示範:輸入頻率、相位與振幅,按下滑鈕
一支 App 多種語言:Android 本地化的資料夾命名規則與實作
App 上架之後,使用者可能來自任何國家。若介面只有單一語言,即便功能再好,也會在語言這一關流失不少潛在用戶。Android 的資源系統把這件事處理得非常優雅:你只要按命
Android Spinner 載入動畫實作指南
當 App 執行網路請求或讀取資料時,使用者需要知道系統正在忙碌。Android 的 ProgressBar 可分為進度條與 Spinner;若任務總長度未知,Spinn