編輯:Android 開發實例
登入畫面存在的目的,是向使用者索取憑證以進入特定應用。這類需求很常見,例如要登入 Facebook、微博等服務。
本章說明如何建立一個登入介面,以及如何處理安全性與錯誤嘗試的問題。
首先必須定義兩個輸入欄位來詢問使用者名稱與密碼,其中密碼欄的 inputType 必須設為密碼型態:
<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" />
接著定義一顆文字為「登入」的按鈕,並設定它的 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 檔的 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 {
// 密碼錯誤
}
}
最後要做的是提供一種安全機制,避免不必要的嘗試。做法是初始化一個變數,每失敗一次就遞減;當它降到 0 時,停用登入按鈕。
int counter = 3;
counter--;
if (counter == 0) {
// 停用按鈕、關閉應用程式等
}
以下範例示範一個登入應用,只給使用者三次機會登入。
| 步驟 | 做法 |
|---|---|
| 1 | 用 Android Studio 建立專案並命名為 LoginScreen,Target SDK 與 Compile With 請選 Android SDK 最新版本或較高 API 等級。 |
| 2 | 修改 src/MainActivity.java 加入必要程式碼。 |
| 3 | 修改 res/layout/activity_main.xml 加入對應的 XML 元件。 |
| 4 | 修改 res/values/string.xml 加入必要字串。 |
| 5 | 執行應用程式,選擇執行中的 Android 裝置安裝並驗證結果。 |
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/textV 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/textV TextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textV 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/textV TextView2"
android:layout_marginLeft="32dp"
android:layout_toRightOf="@+id/textV TextView2"
android:ems="10" >
<requestFocus />
</EditText>
<TextView android:id="@+id/textV TextView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textV TextView2"
android:layout_below="@+id/textV 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/textV 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/textV TextView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textV TextView3"
android:layout_below="@+id/textV TextView3"
android:layout_marginLeft="30dp"
android:layout_marginTop="48dp"
android:text="@string/attempts"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView android:id="@+id/textV TextView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textV TextView1"
android:layout_alignTop="@+id/textV 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 並啟動,若設定與程式都沒問題,會顯示以下模擬器視窗:
在欄位中輸入使用者名稱與密碼後按登入鈕。這裡示範把 admin 當作使用者名稱、Nimda 當作密碼,因此登入失敗:
重複做到第三次,會看到剩餘嘗試次數為 0,而且登入按鈕已被停用。
現在再次打開應用程式,輸入正確的使用者名稱 admin、密碼 admin 並點擊登入,就會成功登入。
需要補充的是,本範例把帳密直接寫在程式碼中,僅供流程示範。實際產品應由後端驗證、全程走 HTTPS,並在伺服器端記錄與限制登入嘗試次數。
Android MediaPlayer 多媒體播放教學:從 create() 到 release()
Android 提供了多種方式來控制音訊與影片檔案及串流的播放,其中最常用的一條路徑就是透過 MediaPlayer 這個類別。 MediaPlayer 是內建媒體播放
Android 手勢(Gesture)處理:從單擊、滑動到多點縮放
觸控螢幕上的操作千變萬化,Android 把幾種常見的動作抽象成「手勢」:點擊、長按、滑動、快速滑動(Fling)、雙指捏合縮放等。應用程式只要攔截這些手勢,就能提供更自
Android 登入介面開發指南:從 XML 版面到鎖定機制
登入畫面通常是一個 App 使用者最先接觸到的部分。無論背後接的是 Facebook、微博還是自建服務,它的責任都一样:收進帳號密碼、驗證、決定放行或退回。本章說明如何打
Android 指南針實作:用方向感測器打造指北 App
想製作一個指北針 App 嗎?Android 的方向感測器(Orientation Sensor)能即時回報裝置朝向,搭配一張指北針圖片與旋轉動畫,就能輕鬆實現。以下分四