如何在 Chaquopy 中直接调用我的函数?

How to call my function directly in Chaquopy?

如何调用我使用 Chaquopy 编写的函数?

我使用了文档中的示例,效果很好。我可以使用 Python 将文本放在标签上。然后我创建了一个按钮并想调用我的函数(例如,my_func):

from demo.chaquopy.pythonactivity import R

from java import static_proxy, Override, jvoid

from android.os import Bundle

from android.support.v7.app import AppCompatActivity


class MainActivity(static_proxy(AppCompatActivity)):

    @Override(jvoid, [Bundle])
    def onCreate(self, state):
        AppCompatActivity.onCreate(self, state)
        self.setContentView(R.layout.activity_main)
        self.findViewById(R.id.label).setText("Hello From Python!")

    def my_func(self):
        self.findViewById(R.id.label).setText("Another text")

我尝试像 Java 函数一样编写 'android:onclick',但它不起作用。我在示例和文档中找不到它。请问有人可以帮忙吗?非常感谢。

要在 XML 中声明侦听器,该方法必须对 Java 可见。您可以像这样使其可见:

from java import method
from android.view import View

class MainActivity(...):

    @method(jvoid, [View])
    def my_func(self):
        ...