简单实现支付密码的页面及输入效果

干我们这行,风吹日晒不到,就怕甲方突发奇想。

今天客户要做一个安全密码前置校验,还要做成支付宝那种效果。ps:android端

心理吐槽了一万遍以后,还是得面对现实。

先用通义问一遍,给了两个方案,要么自己写,要么用第三方库。

向来自己动手的我,先试着把通义给的源码抄下来,运行效果不佳。

转用第三方库,sync后版本又不对,找了多个库,效果都不理想,要么版本不兼容,要么效果不理想。

最后回头用通义的思路自己写。

思路分为三个步骤。

1.页面上用6个textview,用来显示黑点的效果,外加一个edittext,edittext宽高1dp,基本是隐藏的效果,edittext用来接受焦点和输入密码

java 复制代码
 		<LinearLayout
            android: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获取焦点,但是无法拉起输入键盘,要单独写拉起键盘的操作。

java 复制代码
password_input.requestFocus();
showime();
java 复制代码
private void showime(){
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.showSoftInput(password_input, InputMethodManager.SHOW_IMPLICIT);
        }
    }

3.键盘输入后,假密码框有输入效果

java 复制代码
TextWatcher commonTextWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

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

            @Override
            public 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);
java 复制代码
 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);
        }
    }

这样就搞定了,这个思路可以用到各个开发语言下。这个只是思路,具体实现还要补充不少东西。看下效果

相关推荐
三少爷的鞋3 小时前
为什么成熟 Android 项目几乎看不到 IdleHandler?
android
kururunga4 小时前
Android知识点回顾之蓝牙协议
android·蓝牙·车载开发
StarsGreen5 小时前
aosp中的SELinux 策略配置和编译流程
android
方白羽9 小时前
Android 开发中常见的编译加速方案
android·app·gradle
StarsGreen11 小时前
Android VINTF 中 Device Manifest 与 Framework Manifest 详解
android
Name_NaN_None13 小时前
安卓手机远程控制电脑方法(Android/Windows) —— 「小白教程」
android·windows·智能手机·电脑·笔记本
恋猫de小郭13 小时前
Flutter Perfetto:Dart Heap Sampling 终于要接入 Android 原生内存分析了
android·前端·flutter
敢敢のwings1 天前
GR00T 2:当机器人学会先想象未来,再决定怎么动
android·机器人
传奇开心果编程1 天前
【Jetpack Compose娓娓道来】第55课:传感器与设备姿态——把物理世界的数据接进Compose
android·kotlin·移动开发·传感器·jetpack compose·ui框架·声明式ui
传奇开心果编程1 天前
【Jetpack Compose娓娓道来】第52课:蓝牙与位置权限——把“附近”和“在哪”安全地接进Compose
android·kotlin·移动开发·蓝牙·jetpack compose·声明式ui·位置权限