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

Android 自訂 View 打造支付寶風格六位密碼輸入元件

編輯:關於 Android 程式設計

這個範例會實作一個類似支付寶的六位密碼輸入畫面。整體做法是透過 PopupWindow 顯示自訂的 PayView,當使用者輸入完六位數字後,再由回呼把結果傳回 Activity,最後以 Toast 顯示。

自訂 View 的版面配置如下:

自訂密碼輸入版面

密碼格可以由六個 TextView 組成,中間以分隔線切開,下方再放一個自製數字鍵盤。這裡使用 GridView 呈現 0 到 9、刪除鍵與空白鍵。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_pay_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="輸入支付密碼"
        android:textSize="18sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:orientation="horizontal">

        <TextView android:id="@+id/tv_pass1" android:layout_weight="1" ... />
        <TextView android:id="@+id/tv_pass2" android:layout_weight="1" ... />
        <TextView android:id="@+id/tv_pass3" android:layout_weight="1" ... />
        <TextView android:id="@+id/tv_pass4" android:layout_weight="1" ... />
        <TextView android:id="@+id/tv_pass5" android:layout_weight="1" ... />
        <TextView android:id="@+id/tv_pass6" android:layout_weight="1" ... />
    </LinearLayout>

    <GridView
        android:id="@+id/gv_keybord"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="3" />
</LinearLayout>

自訂 PayView 的核心邏輯

PayView 繼承 RelativeLayout,負責初始化六個密碼格、數字鍵盤、取消按鈕與標題。當第六格有輸入時,就觸發完成回呼。

public class PayView extends RelativeLayout {
    private TextView[] mTextViewPsw = new TextView[6];
    private GridView mGridView;
    private String mStringPassword;
    private int currentIndex = -1;

    public PayView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mView = View.inflate(context, R.layout.pay_view, null);
        addView(mView);

        mTextViewPsw[0] = mView.findViewById(R.id.tv_pass1);
        mTextViewPsw[1] = mView.findViewById(R.id.tv_pass2);
        mTextViewPsw[2] = mView.findViewById(R.id.tv_pass3);
        mTextViewPsw[3] = mView.findViewById(R.id.tv_pass4);
        mTextViewPsw[4] = mView.findViewById(R.id.tv_pass5);
        mTextViewPsw[5] = mView.findViewById(R.id.tv_pass6);
        mGridView = mView.findViewById(R.id.gv_keybord);
    }

    public void setOnFinishInput(OnPasswordInputFinish pass) {
        mTextViewPsw[5].addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() == 1) {
                    mStringPassword = "";
                    for (TextView tv : mTextViewPsw) {
                        mStringPassword += tv.getText().toString().trim();
                    }
                    pass.inputFinish();
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
        });
    }

    public interface OnPasswordInputFinish {
        void inputFinish();
    }
}

在 Activity 中顯示 PopupWindow

按下支付按鈕後,建立 PopupWindow 並載入 PayView。完成輸入後,透過 Toast 顯示結果。

public void showPopupWindow() {
    View popView = View.inflate(this, R.layout.pop_window, null);
    mPopupWindow = new PopupWindow(
        popView,
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT
    );

    PayView payView = popView.findViewById(R.id.pv_pop_win);
    payView.setOnFinishInput(new PayView.OnPasswordInputFinish() {
        @Override
        public void inputFinish() {
            Toast.makeText(MainActivity.this,
                payView.getPassword(),
                Toast.LENGTH_SHORT).show();
        }
    });

    mPopupWindow.showAtLocation(
        findViewById(R.id.ll_main),
        Gravity.BOTTOM,
        0,
        0
    );
}

透過自訂 View 與 PopupWindow 的組合,就能做出接近原生支付應用的密碼輸入體驗,同時也方便後續擴充標題、取消鍵、忘記密碼等互動。

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