英文:
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("check", "success")
}
}
from activity_navbar.xml without using NavbarActivity.kt in TW
<TextView
android:onClick="?????"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TEST"
/>
答案1
得分: 1
你可以通过使用自定义绑定适配器来实现这一点。以下是您可以执行此操作的方法:
- 在项目的源代码目录中创建一个名为
BindingAdapters.kt
的文件(如果不存在),并添加以下代码:
import android.view.View
import androidx.databinding.BindingAdapter
@BindingAdapter("onClickTestPrint")
fun View.setTestPrintClickListener(navbarActions: NavbarActions) {
setOnClickListener { navbarActions.testPrint(this) }
}
- 在你的
activity_navbar.xml
中,将onClickTestPrint
属性添加到TextView
中:
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TEST"
app:onClickTestPrint="@{navbarActions}" />
请注意,你需要将 @{navbarActions}
替换为你想要使用的实际 NavbarActions
实例。
- 最后,在你的活动中设置数据绑定(例如
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:
- 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("onClickTestPrint")
fun View.setTestPrintClickListener(navbarActions: NavbarActions) {
setOnClickListener { navbarActions.testPrint(this) }
}
- In your
activity_navbar.xml
, add theonClickTestPrint
attribute to theTextView
:
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TEST"
app:onClickTestPrint="@{navbarActions}" />
Note that you need to replace @{navbarActions}
with the actual instance of NavbarActions
that you want to use.
- Finally, when you set up the data binding in your activity (e.g.,
MainActivity.kt
), make sure to set thenavbarActions
variable with an instance ofNavbarActions
:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论