Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 繪製 QQ 風格登入界面的完整程式碼

編輯:關於 Android 程式設計

先看一下成果:

這個看起來簡單的畫面,實際做下去會碰到四個不大不小的問題:

1、如何為 EditText 設定自定義背景。

2、頁面一打開 EditText 就自動取得焦點。

3、要在取得焦點時就清空 hint,而不是等使用者輸入後才清。

4、清除按鈕該在什麼時機出現(取得焦點且已有輸入內容時)。

以下逐一說明。

一、佈局檔 fragment_main(問題 2)

<!-- android:focusable="true" 與 android:focusableInTouchMode="true"
     這兩個屬性放在根節點,可以中斷 EditText 預設的行為 -->
<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:background="#ECEDF1"
    android:focusable="true"
    android:focusableInTouchMode="true"
    tools:context="com.dragon.android.qqlogin.MainActivity$PlaceholderFragment" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="40dp"
        android:src="@drawable/a" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageView1"
        android:ems="10"
        android:background="@drawable/bg_edittext"
        android:inputType="textPersonName"
        android:gravity="center"
        android:textColor="#6A6A6C"
        android:hint="@string/inaccount"
        android:textColorHint="#ECEDDD" >
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/editText1"
        android:ems="10"
        android:background="@drawable/bg_edittext"
        android:inputType="textPassword"
        android:gravity="center"
        android:textColor="#6A6A6C"
        android:hint="@string/inpwd"
        android:textColorHint="#ECEDDD" >
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/editText2"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/bg_button"
        android:text="@string/button"
        android:gravity="center"
        android:textColor="#F9FAFB" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:padding="10dp" >
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/faillogin"
            android:textColor="#0EB1EF" />
        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="@string/regist"
            android:textColor="#0EB1EF" />
    </LinearLayout>

    <!-- 清除按鈕,預設隱藏 -->
    <Button
        android:id="@+id/button2"
        android:layout_width="16dp"
        android:layout_height="16dp"
        android:layout_alignTop="@id/editText1"
        android:layout_marginTop="15dp"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:background="@drawable/clear"
        android:visibility="invisible" />

    <Button
        android:id="@+id/button3"
        android:layout_width="16dp"
        android:layout_height="16dp"
        android:layout_alignTop="@id/editText2"
        android:layout_marginTop="15dp"
        android:layout_alignLeft="@+id/button2"
        android:background="@drawable/clear"
        android:visibility="invisible" />
</RelativeLayout>

關鍵在根節點的 focusable 與 focusableInTouchMode。把焦點搶在父容器上,就能阻止 EditText 在進入頁面時自動取得焦點與彈出鍵盤。

二、按鈕與輸入框的背景(問題 1)

1. bg_button

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android" >
    <stroke android:width="1px" android:color="#00ACED" />
    <solid android:color="#00ACED" />
    <corners android:radius="10dp" />
</shape>

2. bg_edittext

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android" >
    <stroke android:width="1px" android:color="#ECEDF1" />
    <solid android:color="#F9FAFB" />
    <corners android:radius="10dp" />
    <padding android:top="10dp" android:bottom="10dp"/>
</shape>

用 shape 標籤畫圓角矩形,比切圖更靈活,改顏色或圓角半徑都不用重新出圖。

3. strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">qqloginnew</string>
    <string name="action_settings">Settings</string>
    <string name="button">登錄</string>
    <string name="faillogin">無法登錄?</string>
    <string name="regist">新用戶注冊</string>
    <string name="inaccount">QQ號/手機號/郵箱</string>
    <string name="inpwd">密碼</string>
    <string name="sucess">登錄成功</string>
</resources>

三、MainActivity(問題 3、4)

package com.dragon.android.qqloginnew;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    private EditText editText1;
    private EditText editText2;
    private Button clearButton1;
    private Button clearButton2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

        editText1 = (EditText) findViewById(R.id.editText1);
        editText2 = (EditText) findViewById(R.id.editText2);
        clearButton1 = (Button) findViewById(R.id.button2);
        clearButton2 = (Button) findViewById(R.id.button3);

        // 焦點變更監聽
        editText1.setOnFocusChangeListener(new EditTextListener(clearButton1));
        editText2.setOnFocusChangeListener(new EditTextListener(clearButton2));

        // 清除按鈕點擊監聽
        clearButton1.setOnClickListener(new ClearButtonListener());
        clearButton2.setOnClickListener(new ClearButtonListener());

        // 輸入內容監聽
        editText1.addTextChangedListener(new MyEditTextWatcher(editText1));
        editText2.addTextChangedListener(new MyEditTextWatcher(editText2));
    }

    /** 對 EditText 的內容進行即時監控 */
    class MyEditTextWatcher implements TextWatcher {

        private CharSequence temp;
        private EditText editText;

        public MyEditTextWatcher(EditText editText) {
            this.editText = editText;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // s 為變更前的內容
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // s 為變更後的內容
            temp = s;
        }

        @Override
        public void afterTextChanged(Editable s) {
            // 有內容就顯示清除按鈕,沒內容就隱藏
            if (temp.length() > 0) {
                if (editText == editText1) {
                    clearButton1.setVisibility(View.VISIBLE);
                } else if (editText == editText2) {
                    clearButton2.setVisibility(View.VISIBLE);
                }
            } else {
                if (editText == editText1) {
                    clearButton1.setVisibility(View.INVISIBLE);
                } else if (editText == editText2) {
                    clearButton2.setVisibility(View.INVISIBLE);
                }
            }
        }
    }

    /** 清除按鈕點擊事件 */
    class ClearButtonListener implements OnClickListener {
        @Override
        public void onClick(View view) {
            if (view == clearButton1) {
                editText1.setText("");
            } else if (view == clearButton2) {
                editText2.setText("");
            }
        }
    }

    /** 焦點變更事件 */
    class EditTextListener implements OnFocusChangeListener {

        private Button clear;

        public EditTextListener(Button clear) {
            this.clear = clear;
        }

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            EditText textView = (EditText) v;
            String hint;

            if (hasFocus) {
                // 焦點在這裡且已有內容,清除按鈕可見
                if (!textView.getText().toString().equals("")) {
                    clear.setVisibility(View.VISIBLE);
                }
                // 把 hint 暫存到 tag,再清空 hint
                hint = textView.getHint().toString();
                textView.setTag(hint);
                textView.setHint("");
            } else {
                clear.setVisibility(View.INVISIBLE);
                // 取回暫存的 hint 還原
                hint = textView.getTag().toString();
                textView.setHint(hint);
            }
        }
    }
}

問題 3 的解法就是上面這段:取得焦點時把 hint 存進 setTag,再把 hint 設為空字串;失去焦點時再從 tag 取回還原。用 View 自帶的 tag 當暫存區,不必另外維護一份狀態變數。

問題 4 則由 TextWatcher 與 OnFocusChange 聯手處理:afterTextChanged 負責依內容有無切換清除按鈕的顯示,onFocusChange 負責在切換焦點時同步一次,兩者搭配就能讓按鈕的出現時機完全符合預期。

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