如何在XML的OnClick属性中从另一个类调用函数?

huangapple go评论63阅读模式
英文:

how to call fun from another class in xml OnClick attribute

问题

我需要从NavbarActions.kt中运行testPrint函数,而不使用TW中的NavbarActivity.kt。

class NavbarActions {

    fun testPrint(view: View) {
        Log.i("check", "success")
    }
    
}

从activity_navbar.xml中调用,而不使用NavbarActivity.kt。

<TextView
    android:onClick="testPrint"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="TEST"
/>
英文:

i need to run testPrint from NavbarActions.kt

class NavbarActions {

    fun testPrint(view: View) {
        Log.i(&quot;check&quot;, &quot;success&quot;)
    }
    
}

from activity_navbar.xml without using NavbarActivity.kt in TW

&lt;TextView
    android:onClick=&quot;?????&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:text=&quot;TEST&quot;
/&gt;

答案1

得分: 1

你可以通过使用自定义绑定适配器来实现这一点。以下是您可以执行此操作的方法:

  1. 在项目的源代码目录中创建一个名为 BindingAdapters.kt 的文件(如果不存在),并添加以下代码:
import android.view.View
import androidx.databinding.BindingAdapter

@BindingAdapter("onClickTestPrint")
fun View.setTestPrintClickListener(navbarActions: NavbarActions) {
    setOnClickListener { navbarActions.testPrint(this) }
}
  1. 在你的 activity_navbar.xml 中,将 onClickTestPrint 属性添加到 TextView 中:
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="TEST"
    app:onClickTestPrint="@{navbarActions}" />

请注意,你需要将 @{navbarActions} 替换为你想要使用的实际 NavbarActions 实例。

  1. 最后,在你的活动中设置数据绑定(例如 MainActivity.kt)时,请确保将 navbarActions 变量设置为 NavbarActions 的实例:
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.yourpackage.databinding.ActivityNavbarBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityNavbarBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_navbar)
        binding.navbarActions = NavbarActions()
    }
}

请确保将 com.yourpackage 替换为您项目的实际包名。

现在,当点击“TEST” TextView 时,它将直接调用在活动中的 navbarActions 变量中指定的 NavbarActions 实例的 testPrint 函数。

英文:

You can achieve this by using a custom binding adapter. Here's how you can do it:

  1. Create a file named BindingAdapters.kt (if it doesn't exist) in your project's source directory and add the following code:
import android.view.View
import androidx.databinding.BindingAdapter

@BindingAdapter(&quot;onClickTestPrint&quot;)
fun View.setTestPrintClickListener(navbarActions: NavbarActions) {
    setOnClickListener { navbarActions.testPrint(this) }
}
  1. In your activity_navbar.xml, add the onClickTestPrint attribute to the TextView:
&lt;TextView
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:text=&quot;TEST&quot;
    app:onClickTestPrint=&quot;@{navbarActions}&quot; /&gt;

Note that you need to replace @{navbarActions} with the actual instance of NavbarActions that you want to use.

  1. Finally, when you set up the data binding in your activity (e.g., MainActivity.kt), make sure to set the navbarActions variable with an instance of NavbarActions:
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.yourpackage.databinding.ActivityNavbarBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityNavbarBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_navbar)
        binding.navbarActions = NavbarActions()
    }
}

Make sure to replace com.yourpackage with the actual package name of your project.

Now, when the "TEST" TextView is clicked, it will directly call the testPrint function from the NavbarActions instance specified in the navbarActions variable in your activity.

答案2

得分: 0

你不能。onClick 属性只会在它所在的 Activity 中转化为一个函数。你需要从该活动中调用其他函数。

英文:

You can't. The onClick attribute will only translate to a function in the Activity that it's in. You will need to call the other function from the activity.

huangapple
  • 本文由 发表于 2023年7月11日 14:16:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76659156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定