android 实现密码输入弹窗

前言部分

最近有点忙好久没写啥东西了,正好项目中用到了一个输入密码的弹窗,顺便单独记录下,防止以后需要用. 资源我放在文末,有需要的可下载,或者留邮箱

我是图片

内容部分

内容比较简单:
主要分为几个步骤,如下:

  1. 实现dialog的部分内容.
    • 这里注意一下就是dialog的显示问题,比如,你需要设置一个主题,否则dialog显示的可能会有缝隙;还有就是你需要设置dialog的布局属性来保证dialog不是一个变形的弹窗.
    • 代码如下:
val window = window
        // 设置显示动画
        val wl = window!!.attributes
        setCanceledOnTouchOutside(true)
        window.setWindowAnimations(R.style.main_menu_animstyle)
        wl.x = 0
        wl.y = mContext.windowManager.defaultDisplay.height
        // 以下这两句是为了保证按钮可以水平满屏
        wl.width = ViewGroup.LayoutParams.MATCH_PARENT
        wl.height = ViewGroup.LayoutParams.WRAP_CONTENT
        // 设置显示位置
        onWindowAttributesChanged(wl)

以上基本上就差不多,其他问题应该都比较好解决.

  1. 密码输入的布局

    这里我使用是的GridLayout(表格布局)布局,因为我们要使用的就是表格的样式,但是因为使用了只用21版本以上才能使用的api,所以如果你的app需要兼容到21以下.可以替换成其他布局,比如线性布局.(还有就我觉得直接自定义view会更好,因为以前我绘制过影院选座的组件.感觉这个就是只绘制座位和点击时间那部分代码.)
    代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/ll_password_number_result"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_margin="5dp"
            android:background="@color/white"
            android:orientation="horizontal">

            <TextView style="@style/password_number" />

            <TextView style="@style/password_number" />

            <TextView style="@style/password_number" />

            <TextView style="@style/password_number" />

            <TextView style="@style/password_number" />

            <TextView style="@style/password_number" />
        </LinearLayout>
        <!--键盘部分-->
        <GridLayout
            android:id="@+id/gl_password_numbers"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:columnCount="3"
            android:orientation="horizontal"
            android:rowCount="4">

            <TextView
                style="@style/password_btn"
                android:text="1" />

            <TextView
                style="@style/password_btn"
                android:text="2" />

            <TextView
                style="@style/password_btn"
                android:text="3" />


            <TextView
                style="@style/password_btn"
                android:text="4" />

            <TextView
                style="@style/password_btn"
                android:text="5" />

            <TextView
                style="@style/password_btn"
                android:text="6" />

            <TextView
                style="@style/password_btn"
                android:text="7" />

            <TextView
                style="@style/password_btn"
                android:text="8" />

            <TextView
                style="@style/password_btn"
                android:text="9" />

            <TextView
                style="@style/password_btn"
                android:enabled="false"
                android:text="      " />

            <TextView
                style="@style/password_btn"
                android:text="   0   " />

            <TextView
                style="@style/password_btn"
                android:text="delete" />
        </GridLayout>
    </LinearLayout>


</LinearLayout>

代码简单明了.

  1. 密码操作的部分,主要是一些判断逻辑,直接贴出来代码吧,
  fun setData() {
        for (i in 0 until gl_password_numbers.childCount) {
            gl_password_numbers.getChildAt(i).setOnClickListener { v ->
                if (v is TextView) {
                    val s = v.text.toString().trim { it <= ' ' }
                    if (s.length == 1) {
                        if (count < 6) {
                            val childAt = ll_password_number_result.getChildAt(count) as TextView
                            childAt.text = s
                            passwordList.add(s)
                            if (count == 5) {
                                completeListener.onCompleteListener(passwordList)
                            }
                            count++
                        }

                    } else {
                        if (count > 0) {
                            count--
                            val childAt = ll_password_number_result.getChildAt(count) as TextView
                            childAt.text = ""
                            passwordList.removeAt(count)
                        }
                    }
                }

            }
        }

    }
  1. 加入回调

    回调就很简单了,把拿到的数字返回回去就可以了.
    . 没了

内容比较简单有需要的直接拿去改一改应该就可以了.
demo下载地址

结束部分

由于在忙新的商城项目,最近也没怎么看东西,目前计划如果有时间会学习flutter相关.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章