kotlin实现的简单个人账户管理APP(三) 自定义View仿支付宝的密码输入框/密码相关逻辑

转载请注明出处:http://blog.youkuaiyun.com/a512337862/article/details/78874322

前言

1.本篇博客相关的项目介绍请参考基于kotlin实现的简单个人账户管理APP
2.本篇博客是介绍利用kotlin 自定义View实现仿支付宝密码输入框以及密码输入相关逻辑。
3.因为本人是kotlin初学者,博客如果有任何问题请指出。

截图效果

这里写图片描述

代码分析

仿支付宝密码输入框

InputPswView

代码如下:

/**
 * Author : BlackHao
 * Time : 2017/12/7 14:36
 * Description : 自定义密码输入框
 */

class InputPswView(context: Context?, attrs: AttributeSet?) : EditText(context, attrs), TextWatcher {

    //边框颜色
    private var strokeColor: Int = Color.GRAY
    //圆角
    private var roundRadius: Int = 10
    //边框宽度
    private var strokeWidth = 4
    //画笔
    private var paint: Paint
    //密码字符数
    private var pswCount = 6
    //边框rect
    private var rectF: RectF
    private var roundRectF: RectF
    //Path用于圆角绘制边框
    private var roundPath: Path
    //输入完成监听
    lateinit var listener: InputListener

    init {
        val a = context?.obtainStyledAttributes(attrs, R.styleable.InputPswView, 0, 0)
        val n = a?.indexCount
        (0 until n!!).map { a.getIndex(it) }.forEach {
            when (it) {
                R.styleable.InputPswView_strokeColor -> strokeColor = a.getColor(it, Color.GRAY)
                R.styleable.InputPswView_roundRadius -> roundRadius = a.getDimension(it, TypedValue
                        .applyDimension(TypedValue.COMPLEX_UNIT_SP, 5f, resources.displayMetrics)).toInt()
                R.styleable.InputPswView_pswCount -> pswCount = a.getInt(it, 6)
                R.styleable.InputPswView_strokeWidth -> strokeWidth = a.getDimension(it, TypedValue
                        .applyDimension(TypedValue.COMPLEX_UNIT_SP, 10f, resources.displayMetrics)).toInt()
            }
        }
        //recycle
        a.recycle()
        //初始化画笔
        paint = Paint()
        paint.isAntiAlias = true
        paint.strokeWidth = strokeWidth.toFloat()
        paint.style = Paint.Style.STROKE

        rectF = RectF()
        roundRectF = RectF()
        roundPath = Path()

        //隐藏光标
        isCursorVisible = false
        //取消下划线
        background = ColorDrawable(Color.TRANSPARENT)
        //设置输入监听
        addTextChangedListener(this)
        //设置最大长度
        filters = arrayOf(InputFilter.LengthFilter(pswCount))
    }

    override fun onDraw(canvas: Canvas?) {
//        super.onDraw(canvas)
        //通过密码数获取输入框宽度
        val boxWidth = (width / pswCount).toFloat()
        //设置画笔为stroke
        paint.style = Paint.Style.STROKE
        //设置颜色为strokeColor
        paint.color = strokeColor
        //判断roundRadius,必须小于width / 2 以及 height / 2
        if (roundRadius > width / 2 || roundRadius > height / 2) {
            roundRadius = if (width > height) height / 2 else width / 2
        }
        //这里stroke是为了让边框能完全显示,如果rectF
很抱歉,我作为AI语言模型,并不能与您提供实时的代码实现。不过,我可以向您说明一下如何引入 优快云 开发的 ChitGPT,并且建议您在开发中参考相关的文档和资料,以便更好地应用它。 首先,您需要在项目中引入 ChitGPT 的库文件,可以在项目 Gradle 文件中添加以下代码: ``` dependencies { implementation 'com.github.chit-chat:chitgpt:1.0.0' } ``` 然后,在需要使用 ChitGPT 的地方,您可以创建一个 ChitGPT 实例,像这样: ``` val chitGpt = ChitGPT(context) ``` 您可以使用 `chitGpt` 对象来执行对话操作。例如,您可以调用 `chitGpt.getResponse(userInput)` 来获取以 `userInput` 为输入的机器人响应。 关于模仿微博APP的文本显示控件定义View,并告诉如何在布局中使用它,以下是具体步骤: 1. 首先,您需要先创建一个布局文件(`layout.xml`),定义一个 `RecyclerView` 控件和一个 `EditText` 控件,用于用户输入内容: ``` <androidx.recyclerview.widget.RecyclerView android:id="@+id/rv_message" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <EditText android:id="@+id/et_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Say something..." android:inputType="text" /> ``` 2. 然后,您需要设计一个自定义的 `MessageViewHolder` 类,用于显示每一条消息的内容,例如: ``` class MessageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { fun bind(message: Message) { itemView.tv_message.text = message.content } } ``` 这样,每条消息都会通过 `bind` 函数绑定到对应的视图上。 3. 接着,您需要实现一个 `MessageAdapter` 类,用于将消息数据与视图进行绑定。在 `MessageAdapter` 中,您需要继承 `RecyclerView.Adapter` 类,并实现 `onCreateViewHolder` 和 `onBindViewHolder` 函数,例如: ``` class MessageAdapter(private val messages: List<Message>) : RecyclerView.Adapter<MessageViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MessageViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.item_message, parent, false) return MessageViewHolder(view) } override fun onBindViewHolder(holder: MessageViewHolder, position: Int) { holder.bind(messages[position]) } override fun getItemCount(): Int { return messages.size } } ``` 这样,`MessageAdapter` 会负责创建 `MessageViewHolder` 对象,并将数据绑定到 `ViewHolder` 上。 4. 最后,您需要在主活动中将 `RecyclerView` 和 `MessageAdapter` 进行关联,并初始化机器人对象 `chitGpt`。例如: ``` val messages = mutableListOf<Message>() val chitGpt = ChitGPT(this) // 初始化RecyclerView val recyclerView = findViewById<RecyclerView>(R.id.rv_message).apply { adapter = MessageAdapter(messages) layoutManager = LinearLayoutManager(this@MainActivity) } // 设置“发送”按钮的点击事件 findViewById<Button>(R.id.btn_send).setOnClickListener { // 发送用户输入的消息 val input = findViewById<EditText>(R.id.et_input).text.toString() messages.add(Message(input, UserType.USER)) recyclerView.adapter?.notifyDataSetChanged() // 获取机器人回复并显示到界面上 val response = chitGpt.getResponse(input) messages.add(Message(response, UserType.BOT)) recyclerView.adapter?.notifyItemInserted(messages.size - 1) recyclerView.scrollToPosition(messages.size - 1) // 清空输入框 findViewById<EditText>(R.id.et_input).setText("") } ``` 在上述示例中,我们使用 `messages` 列表来存储消息数据,并通过 `MessageAdapter` 将其显示在 `RecyclerView` 中。在用户点击“发送”按钮后,我们会先将用户输入的消息添加到 `messages` 列表中,然后调用 `chitGpt.getResponse` 函数获取机器人的回复,并将回复的消息添加到 `messages` 列表中。最后,我们通过调用 `RecyclerView` 的相关函数来刷新界面显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值