程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何 mvvm 按钮 OnClick 事件?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何 mvvm 按钮 OnClick 事件??

开发过程中遇到如何 mvvm 按钮 OnClick 事件?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何 mvvm 按钮 OnClick 事件?的解决方法建议,希望对你解决如何 mvvm 按钮 OnClick 事件?有所启发或帮助;

当我点击按钮时,我尝试更改片段,但 Timber 似乎没有点击自己。

我想通过在xml中的viewmodel中应用click函数来处理它,而不是在activity中处理它。

我用谷歌搜索了很多,但我找不到正确的答案,所以如果你有任何好的数据,你可以分享它。

我的代码

按钮布局 Xml

<?xml version="1.0" enCoding="utf-8"?>
<layout xmlns:androID="http://scheR_436_11845@as.androID.com/apk/res/androID"
    xmlns:app="http://scheR_436_11845@as.androID.com/apk/res-auto"
    xmlns:tools="http://scheR_436_11845@as.androID.com/tools">

    <data>

        <variable
            name="text"
            type="String" />

        <variable
            name="onClick"
            type="androID.vIEw.VIEw.onClickListener" />

    </data>

    <TextVIEw
        androID:ID="@+ID/textvIEw_positive_button"
        androID:layout_wIDth="match_parent"
        androID:layout_height="match_parent"
        androID:BACkground="@drawable/shape_positive_button"
        androID:BACkgroundTint="@color/main_blue"
        androID:gravity="center"
        androID:text="@{text}"
        androID:textcolor="@color/colorPriMary"
        androID:textSize="18dp"
        androID:onClick="@{onClick ?? null}"
        tools:text="@String/confirm" />
</layout>

主 SignActivity .Xml

<data>

    <import type="enums.SignUpStep" />

    <variable
        name="signUpviewmodel"
        type="viewmodel.SignUpviewmodel" />
</data>

<include
    androID:ID="@+ID/include"
    layout="@layout/layout_positive_button"
    androID:layout_wIDth="match_parent"
    androID:layout_height="60dp"
    androID:layout_margin="5dp"
    app:layout_consTraintEnd_toEndOf="parent"
    app:layout_consTraintStart_toStartOf="parent"
    app:layout_consTrainttop_toBottomOf="@+ID/recommender_consTraint"
    app:onSafeClick="@{() ->signUpviewmodel.moveStep(SignUpStep.passworD)}"
    />

绑定适配器

@BindingAdapter("onSafeClick")
fun VIEw.setonSafeClickListener(clickListener: VIEw.onClickListener?) {
    clickListener?.also {
        setonClickListener(OnSafeClickListener(it))
    } ?: setonClickListener(null)
}

安全点击监听器

abstract class OnSafeClickListener(
        private val clickDelaymilliSecond: Long = CliCK_DELAY_TIME
) : VIEw.onClickListener {

    companion object {
        private const val CliCK_DELAY_TIME = 1000L
    }

    private var lastClickTime = 0L

    protected abstract fun onSingleClick(vIEw: VIEw)

    overrIDe fun onClick(vIEw: VIEw) {
        val Now = System.currentTimeMillis()
        if (Now - lastClickTime > clickDelaymilliSecond) {
            onSingleClick(vIEw)
            lastClickTime = Now
        }
    }
}

什么都行。能给点建议吗?

我不知道为什么它没有点击按钮。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的如何 mvvm 按钮 OnClick 事件?全部内容,希望文章能够帮你解决如何 mvvm 按钮 OnClick 事件?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。