編輯:Android 開發實例
登入畫面是許多應用的第一道門。使用者要登入社群平台、雲端硬碟、內部系統,都得先通過這關。本章拆解它的三個組成部分:輸入介面、帳密比對,以及防止暴力嘗試的次數鎖。
帳號欄用一般 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" />
登入鈕透過 android:onClick 綁定事件,值就是 Activity 中方法的名稱:
<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() 比較內容:
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 | 執行並驗證。 |
主活動 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.textViedw5);
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/textViedw1" 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" />
<TextView android:id="@+id/textViedw2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@+id/textViedw1"
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/textViedw2"
android:layout_toRightOf="@+id/textViedw2" android:ems="10" >
<requestFocus />
</EditText>
<TextView android:id="@+id/textViedw3" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@+id/textViedw2"
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/textViedw3"
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/textViedw4" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@+id/textViedw3"
android:layout_marginTop="48dp" android:text="@string/attempts" />
<TextView android:id="@+id/textViedw5" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="TextView" />
</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:
<?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" >
<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,密碼刻意顛倒輸入:
再錯一次,剩餘次數變成 0,登入鈕同時被停用:
重新開啟 App 讓計數器重置,輸入正確的帳號與密碼後按下登入,即可成功通過:
要特別說明的是,範例把帳密寫死在程式碼中,純粹為了示範流程。正式專案請務必把驗證放到伺服器端,並全程走 HTTPS;另外,次數鎖定只是防線之一,還應搭配鎖定時間、IP 監控與通知機制才夠穩固。
Android 教學:實作介面跳動提示動畫效果
本文要說明如何實作一個「介面跳動提示」的動畫效果,藉由讓頁面上緣微微跳動,暗示使用者可以往上拉動來切換頁面。先來看效果圖:先來看版面配置: <RelativeLay
Android 讀取短信並轉存到 SQLite 的實現
系統短信本身也存在 SQLite 數據庫裡,但出於安全機制,Android 不允許其他應用直接訪問。繞過這個限制的正規做法是利用 ContentProvider:通過 c
Android 入門:用 TabHost 與 TabWidget 做分頁標籤
Tab 是行動介面裡最直覺的分頁方式,能把大量內容收進同一個螢幕,讓使用者左右切換而不必層層進入。代價是開發複雜度上升,所以建議真的有必要時再採用。 Android 的
Android 應用本地化:values-local 與多語系字串設定
Android 的本地化機制主要是透過不同資源資料夾來達成。當系統語言改變時,Android 會優先載入符合目前語言環境的資源。 資料夾命名規則 本地化字串通常放在 re