如何在一个视图上调用多个onClick,并将其与数据绑定和ViewModel结合使用?

问题描述

我试图通过onClick调用多个方法,并将其通过ViewModel传递给数据绑定。 我在这样的xml文件中调用视图

android:onClick="@{() -> gameViewModel.increaseScore()}"

,我想添加另一种方法,但是收到错误“ Duplicate attribute onClick”。 从理论上讲,我在这里找到了解决方案-Multiple click listeners on buttons,但我认为这不是摆脱setOnClickListners的目的,现在要实现View.OnClickListener侦听器并以非常相似的方式进行。有什么聪明的方法可以将所有这些组合在一起吗?

解决方法

使用Kotlin,您可以做类似的事情

 function checkDetailsFields($requested,$errormessage)
    {
        if (empty($this->vbulletin->GPC['fullname']) or empty($this->vbulletin->GPC['address_1']) or
            empty($this->vbulletin->GPC['address_city']) or empty($this->vbulletin->GPC['address_zip']) or
            empty($this->vbulletin->GPC['country']) or empty($this->vbulletin->GPC['phone']))
        {
            //$GLOBALS['error'] = $errormessage;
            eval(standard_error($errormessage));
            //$this->handleRequest($requested);
            return false;
        }
        return true;
    }

function buildInfoArray()
    {
        return array('fullname' => $this->vbulletin->GPC['fullname'],'address_1' => $this->
            vbulletin->GPC['address_1'],'address_2' => $this->vbulletin->GPC['address_2'],'address_city' => $this->vbulletin->GPC['address_city'],'address_state' => $this->
            vbulletin->GPC['address_state'],'address_zip' => $this->vbulletin->GPC['address_zip'],'country' => $this->vbulletin->GPC['country'],'phone' => $this->vbulletin->GPC['phone'],'company' => $this->vbulletin->GPC['company']);
    }

function setCustomerNumber($ma_info,$usergroup = '',$usevb = true,$userinfo = '')
    {
        if ($usevb == false)
        {
            $this->vbulletin->userinfo = &$userinfo;
        }
        $fcust = $this->fields['custnum'];
        $fpass = $this->fields['mpassword'];
        $finfo = $this->fields['info'];
        $userdm = datamanager_init('User',$this->vbulletin,ERRTYPE_ARRAY);
        $userinfo = fetch_userinfo($this->vbulletin->userinfo['userid']);
        $userdm->set_existing($userinfo);
        if (!$this->vbulletin->userinfo["$fcust"] and !$this->vbulletin->userinfo["$fpass"])
        {
            $rand = rand($this->vbulletin->options['memarea_numstart'],$this->vbulletin->
                options['memarea_numend']);
            $num = $this->vbulletin->options['custnum_prefix'] . substr(md5($rand),$this->
                vbulletin->options['memarea_custnumleng'] - strlen($this->vbulletin->options['custnum_prefix']));
            $userdm->set($fcust,$num);
            $pass = substr(md5(time() . $num . $rand . rand(0,2000)),$this->vbulletin->
                options['memarea_custnumleng']);
            $userdm->set($fpass,$pass);
            $this->sendCustomerInfo($this->vbulletin->userinfo['userid'],$this->vbulletin->
                userinfo['username'],$this->vbulletin->userinfo['email'],$num,$pass);
        }
        if ($usergroup or $usergroup !== '' or $usergroup !== '0')
        {
            if ($usergroup != $this->vbulletin->userinfo['usergroupid'])
            {
            $ma_info['oldgroup'] = $this->vbulletin->userinfo['usergroupid'];
            $userdm->set('usergroupid',$usergroup);
            }
        }
        if ($ma_info)
        {
            $ma_info = serialize($ma_info);
            $userdm->set($finfo,$ma_info);
        }

        $userdm->pre_save();
        if (count($userdm->errors) == 0)
        {
            $userdm->save();
            return true;
        }
        else
        {
            var_dump($userdm->errors);
            return false;
        }
    }

然后在xml中像这样使用它:

@BindingAdapter("customOnClickListener")
fun View.customOnClickListener(viewModel: YourViewModel) {
    setOnClickListener {
        viewModel.actionOne()
        viewModel.actionActionTwo()
        //action three...
    }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...