干我們這行,風吹日曬不到,就怕甲方突發奇想。
今天客戶要做一個安全密碼前置校驗,還要做成支付寶那種效果。ps:android端
心理吐槽了一萬遍以后,還是得面對現實。
先用通義問一遍,給了兩個方案,要么自己寫,要么用第三方庫。
向來自己動手的我,先試著把通義給的源碼抄下來,運行效果不佳。
轉用第三方庫,sync后版本又不對,找了多個庫,效果都不理想,要么版本不兼容,要么效果不理想。
最后回頭用通義的思路自己寫。
思路分為三個步驟。
1.頁面上用6個textview,用來顯示黑點的效果,外加一個edittext,edittext寬高1dp,基本是隱藏的效果,edittext用來接受焦點和輸入密碼
<LinearLayoutandroid:id="@+id/ll_password"android:layout_width="match_parent"android:layout_height="50dp"android:orientation="horizontal" ><TextView android:id="@+id/tv1" style="@style/PwdBox" /><TextView android:id="@+id/tv2" style="@style/PwdBox" /><TextView android:id="@+id/tv3" style="@style/PwdBox" /><TextView android:id="@+id/tv4" style="@style/PwdBox" /><TextView android:id="@+id/tv5" style="@style/PwdBox" /><TextView android:id="@+id/tv6" style="@style/PwdBox" /></LinearLayout><EditText android:id="@+id/password_input"android:inputType="number"android:maxLength="6"android:layout_width="match_parent"android:layout_height="1dp"></EditText>
2.點擊textview,使edittext獲取焦點,但是無法拉起輸入鍵盤,要單獨寫拉起鍵盤的操作。
password_input.requestFocus();
showime();
private void showime(){InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);if (imm != null) {imm.showSoftInput(password_input, InputMethodManager.SHOW_IMPLICIT);}}
3.鍵盤輸入后,假密碼框有輸入效果
TextWatcher commonTextWatcher = new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void afterTextChanged(Editable s) { Log.d("EditText", "內容變化: " + s.toString());String password=s.toString();for(int i=0;i<tvs.length;i++) {tvs[i].setText("");}for(int i=0;i<password.length();i++) {tvs[i].setText("●");}if(password.length()==6 ){if(password.equals("000000")){ //方便書寫,應該通過接口驗證hideime(); //隱藏鍵盤}else{//未通過驗證的處理邏輯}}}};password_input.addTextChangedListener(commonTextWatcher);
private void hideime(){InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);System.out.println(getCurrentFocus());if (imm != null && getCurrentFocus() != null) {imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);}}
這樣就搞定了,這個思路可以用到各個開發語言下。這個只是思路,具體實現還要補充不少東西。看下效果