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

Android 支付密碼輸入框的自定義實現

編輯:關於 Android 程式設計

先看效果:

實現思路

關鍵點是:真正接收輸入的仍然是 EditText,但顯示圓點的並不是它,而是覆蓋在下層的六個 ImageView。做法是先用 RelativeLayout 疊放,把 EditText 的背景設為透明蓋在上面,這樣系統鍵盤照常彈出,視覺上卻只看得到圓點。

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" android:background="@android:color/white"> <ImageView android:id="@+id/item_password_iv1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:src="@mipmap/nopassword"/> <ImageView android:id="@+id/item_password_iv2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:src="@mipmap/nopassword"/> <ImageView android:id="@+id/item_password_iv3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:src="@mipmap/nopassword"/> <ImageView android:id="@+id/item_password_iv4" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:src="@mipmap/nopassword"/> <ImageView android:id="@+id/item_password_iv5" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:src="@mipmap/nopassword"/> <ImageView android:id="@+id/item_password_iv6" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:src="@mipmap/nopassword"/> </LinearLayout> <EditText android:id="@+id/item_edittext" android:layout_width="match_parent" android:layout_height="50dp" android:background="@android:color/transparent"/> </RelativeLayout>

自定義控件 ItemPasswordLayout

這個控件要解決三件事:隱藏光標、把每次輸入的字符存進 StringBuffer 後清空 EditText、以及處理刪除鍵的邏輯。

 /** * 密碼輸入框的控件布局 */ public class ItemPasswordLayout extends RelativeLayout{ private EditText editText; private ImageView[] imageViews;//使用一個數組存儲密碼框 private StringBuffer stringBuffer = new StringBuffer();//存儲密碼字符 private int count = 6; private String strPassword;//密碼字符串 public ItemPasswordLayout(Context context) { this(context,null); } public ItemPasswordLayout(Context context, AttributeSet attrs) { this(context, attrs,0); } public ItemPasswordLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); imageViews = new ImageView[6]; View view = View.inflate(context, R.layout.item_password,this); editText = (EditText) findViewById(R.id.item_edittext); imageViews[0] = (ImageView) findViewById(R.id.item_password_iv1); imageViews[1] = (ImageView) findViewById(R.id.item_password_iv2); imageViews[2] = (ImageView) findViewById(R.id.item_password_iv3); imageViews[3] = (ImageView) findViewById(R.id.item_password_iv4); imageViews[4] = (ImageView) findViewById(R.id.item_password_iv5); imageViews[5] = (ImageView) findViewById(R.id.item_password_iv6); editText.setCursorVisible(false);//將光標隱藏 setListener(); } private void setListener() { editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { //重點 如果字符不為""時才進行操作 if (!editable.toString().equals("")) { if (stringBuffer.length()>5){ //當密碼長度大於5位時edittext置空 editText.setText(""); return; }else { //將文字添加到StringBuffer中 stringBuffer.append(editable); editText.setText("");//添加後將EditText置空 造成沒有文字輸入的錯覺 Log.e("TAG", "afterTextChanged: stringBuffer is "+stringBuffer); count = stringBuffer.length();//記錄stringbuffer的長度 strPassword = stringBuffer.toString(); if (stringBuffer.length()==6){ //文字長度為6 則調用完成輸入的監聽 if (inputCompleteListener!=null){ inputCompleteListener.inputComplete(); } } } for (int i =0;i<stringBuffer.length();i++){ imageViews[i].setImageResource(R.mipmap.ispassword); } } } }); editText.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) { if (onKeyDelete()) return true; return true; } return false; } }); } public boolean onKeyDelete() { if (count==0){ count = 6; return true; } if (stringBuffer.length()>0){ //刪除相應位置的字符 stringBuffer.delete((count-1),count); count--; strPassword = stringBuffer.toString(); imageViews[stringBuffer.length()].setImageResource(R.mipmap.nopassword); } return false; } private InputCompleteListener inputCompleteListener; public void setInputCompleteListener(InputCompleteListener inputCompleteListener) { this.inputCompleteListener = inputCompleteListener; } public interface InputCompleteListener{ void inputComplete(); } public EditText getEditText() { return editText; } /** * 獲取密碼 */ public String getStrPassword() { return strPassword; } public void setContent(String content){ editText.setText(content); } }

核心技巧在 afterTextChanged 裡:一旦不為空就把字符追加到 StringBuffer,然後立刻 setText("") 清空輸入框。這樣做的好處是 EditText 內部始終為空,不會出現光標移動、超出最大長度等一連串邊界問題;同時清空會再次觸發 afterTextChanged,此時 editable 為空,靠最外層的判斷擋住即可。

刪除鍵則通過 onKey 攔截 KEYCODE_DEL,在 StringBuffer 中移除末位字符並把對應 ImageView 換回空圓點。

使用方式

布局中直接聲明:

 <com.example.went_gone.demo.view.ItemPasswordLayout android:id="@+id/act_zhifubao_IPLayout" android:layout_width="match_parent" android:layout_height="wrap_content"> </com.example.went_gone.demo.view.ItemPasswordLayout>

Activity 中註冊完成監聽:

 itemPasswordLayout = (ItemPasswordLayout) findViewById(R.id.act_zhifubao_IPLayout); itemPasswordLayout.setInputCompleteListener(new ItemPasswordLayout.InputCompleteListener() { @Override public void inputComplete() { Toast.makeText(ZhifubaoActivity.this, "密碼是:"+itemPasswordLayout.getStrPassword(), Toast.LENGTH_SHORT).show(); } });

實際項目中還可以再往前一步:把自定義鍵盤替換系統鍵盤、對密碼做加密處理、輸錯時加抖動動畫,讓交互更接近成熟支付產品的樣子。

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