編輯:關於 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>
這個控件要解決三件事:隱藏光標、把每次輸入的字符存進 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(); } });
實際項目中還可以再往前一步:把自定義鍵盤替換系統鍵盤、對密碼做加密處理、輸錯時加抖動動畫,讓交互更接近成熟支付產品的樣子。
完全理解 Android 事件分發機制
前言 這篇文章更像一篇知識概括。相比直接堆砌源碼(容易讓新手望而生畏),筆者更傾向於通過實例的 log 輸出,把 Android 事件分發講清楚。 重要函數 本文主要提
Android 自定義組件屬性實作:背景圖與筆觸大小設定
自定義組件要能在 XML 設定屬性,需要三個步驟:宣告 styleable、在布局使用命名空間、於 Java 端解析屬性。 首先在 res/values 建立 myvi
Android JNI 入門完整實例:從靜態註冊到動態註冊
一、JNI 是什麼 JNI 是 Java Native Interface 的縮寫,中文叫「Java 本地調用」,是一套本地程式設計介面。它讓跑在 Java 虛擬機裡的
動手實作 Android TreeView:用 ListView 拼出多層樹狀目錄
多數作業系統都提供了 TreeView 控制項來呈現樹狀結構,但 Google 在設計 Android 時考量到手機以觸控為主、手指點選層層展開的節點並不順手,因此只提供