編輯:關於 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 繼承 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();
}
}
按下支付按鈕後,建立 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 的組合,就能做出接近原生支付應用的密碼輸入體驗,同時也方便後續擴充標題、取消鍵、忘記密碼等互動。
動手實作 Android TreeView:用 ListView 拼出多層樹狀目錄
多數作業系統都提供了 TreeView 控制項來呈現樹狀結構,但 Google 在設計 Android 時考量到手機以觸控為主、手指點選層層展開的節點並不順手,因此只提供
用 Timer 讓 WebView 進度條勻速推進
用 WebView 載入網頁時,若遇到網路較慢,畫面會出現一片空白,體驗很差。加上進度條是標準解法,但直接把 onProgressChanged() 的數值塞給 Prog
Android 逆向基礎:SO 與 ELF 文件格式詳解
在 Android 逆向分析中,native 層的 SO 文件經常是重點與難點。Android 中的 SO 文件本質上是 ELF 文件,因此理解 ELF 格式,是分析 S
Android 系統架構之微服務架構
前段時間我們翻譯的《軟件架構模式》對外發布後收穫了大家的一致好評。書中講述了五種經典、流行的軟件架構模式,並分析了各自的實現與優缺點,為開發工作提供了寶貴的指導。不過它也