Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 登入畫面與錯誤嘗試管理

編輯:Android 開發實例

登入畫面是幾乎每個需要帳號服務的 App 必備的一環。使用者在此輸入帳號與密碼,系統驗證通過後才允許進入——從 Facebook、微博等社群平台,到各種自製 App 皆然。本章會帶你實作登入介面,並妥善處理安全性與錯誤嘗試的問題。

第一步,版面配置裡要有兩個輸入欄位來收集帳號與密碼。密碼欄位務必把 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 端的 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 }

範例

以下範例打造一個僅允許三次登入嘗試的 App,你可以在實體裝置或模擬器上運行驗證。

步驟 說明 1 使用 Android Studio 建立專案並命名為 LoginScreen,建立時請確認目標 SDK 對應 Android SDK 最新版本或更高階 API。 3 修改 src/MainActivity.java,加入必要程式碼 4 修改 res/layout/activity_main,補上對應的 XML 元件 5 修改 res/values/string.xml,加入所需字串 6 運行 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/mL 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/mL android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/mL 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/mL android:layout_marginLeft="32dp" android:layout_toRightOf="@+id/mL android:ems="10" > <requestFocus /> </EditText> <TextView android:id="@+id/mL android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/mL android:layout_below="@+id/mL 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/mL 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/mL android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/mL android:layout_below="@+id/mL android:layout_marginLeft="30dp" android:layout_marginTop="48dp" android:text="@string/attempts" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/mL android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/mL android:layout_alignTop="@+id/mL 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 並啟動,若一切正常,模擬器會顯示如下畫面:

Android Login Application TutZ喎?/kf/ware/vc/vcmlhbA==" data-ke-="" src="/uploadfile/2016/0203/20160203103046962.jpg" />

在欄位輸入帳號與密碼後按下登入。若把帳號填 admin、密碼故意填成 Nimda,就會登入失敗,如下圖:

連續失敗三次後,剩餘次數歸零,登入按鈕也隨之停用。

重新開啟 App,這次輸入正確的帳號 admin、密碼 admin 並按下登入,便能成功登入。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved