編輯:Android 開發實例
登入畫面是許多 App 的第一道門檻,無論是自家服務的帳號,還是 Facebook、微博這類第三方授權。它看似簡單,實際上要顧到的細節不少:密碼必須遮蔽、錯誤要給明確回饋、還得防止使用者被暴力猜密碼。
本章就從零開始,一步步把這些環節做出來。
需要兩個 EditText,一個收帳號,一個收密碼。關鍵在於密碼欄位要把 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:inputType="textVisiblePassword" />
宣告一顆顯示登入文字的 Button,並透過 onClick 屬性綁定事件;接著在 Java 檔中實作同名的方法:
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="login"
android:text="@string/Login" />
在 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")){
//correcct password
}else{
//wrong password
}
}
最後補上安全機制。宣告一個計數變數,每失敗一次就遞減,歸零時直接停用登入按鈕:
int counter = 3;
counter--;
if(counter==0){
//disble the button, close the application e.t.c
}
以下範例打造一個僅允許三次嘗試的登入畫面,並即時顯示剩餘次數。
| 步驟 | 說明 |
|---|---|
| 1 | 用 Android Studio 建立專案並命名為 LoginScreen,Target SDK 選擇較新版本 |
| 2 | 修改 src/MainActivity.java,加入必要程式碼 |
| 3 | 修改 res/layout/activity_main.xml,加入對應元件 |
| 4 | 修改 res/values/string.xml,補上字串資源 |
| 5 | 執行 App,選擇真機或模擬器安裝並驗證結果 |
主 Activity 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) {
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/testsView1"
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/testsView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/testsView1"
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/testsView2"
android:layout_marginLeft="32dp"
android:layout_toRightOf="@+id/testsView2"
android:ems="10" >
<requestFocus />
</EditText>
<TextView android:id="@+id/testsView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/testsView2"
android:layout_below="@+id/testsView2"
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/testsView3"
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/testsView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/testsView3"
android:layout_below="@+id/editText2"
android:layout_marginLeft="30dp"
android:layout_marginTop="48dp"
android:text="@string/attempts"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView android:id="@+id/testsView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/testsView1"
android:layout_alignTop="@+id/testsView4"
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>
執行應用程式,登入畫面如下:
在帳號欄輸入 admin,密碼欄故意輸入 nimda,按下登入鈕,會看到錯誤提示,剩餘次數同步遞減:
連續犯錯兩次之後,剩餘次數歸零,登入按鈕被停用:
重新啟動 App(計數變數重設),這次輸入正確的 admin/admin 並按下登入,即會成功通過驗證:
最後提醒:範例把帳密寫死在程式碼中,僅供教學。真實的應用請務必把驗證交給伺服器端,透過 HTTPS 傳輸,並在每次失敗後加入間隔冷卻時間——單純把計數器放在記憶體裡,App 一重啟就會被重置,防不了有心人士。
Android 多語言本地化完整指南
一款 Android 應用往往會在不同國家、各種裝置上運行,使用者來自各地。要讓應用更具親和力,就必須針對當前語言環境妥善處理文字、數字、檔案等內容,這正是本地化的核心目
Android 短信驗證碼注冊功能全流程實現與手機號正則校驗
注冊賬號時「填手機號 → 收驗證碼 → 提交注冊」這條路徑,幾乎每個 App 都有。本文以一個注冊頁為例,把網絡請求分層、驗證碼倒計時、Handler 線程通信、弱引用防
Android 多語言支援:用 values-it 資料夾讓 App 自動說義大利語
你的 App 可能同時裝在東京、羅馬與墨西哥城的手機上。如果介面永遠是同一種語言,等於自願放棄那些市場。Android 在資源系統的設計上早就想過這件事:只要依命名慣例準
Android 應用本地化實作:靠資源資料夾自動切換語言
同一支 App 可能同時被東京、米蘭、聖保羅的使用者安裝。要讓每個人都用得順手,App 就得跟著裝置的語言環境調整文字、數字與檔案格式,這就是本地化。Android 在這