如何调用Button ellement的另一个activity / class onClick()中定义的方法? [英] How to call a method defined in another activity/class onClick() of a Button ellement?

查看:219
本文介绍了如何调用Button ellement的另一个activity / class onClick()中定义的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android Studio(最低API为15 )进行主要活动的布局,并在其上定义了一些XML按钮。

I am working on the Layout of my main activity with Android Studio (lowest API is 15), and have defined a few XML Buttons on it.

该程序的想法是通过添加,显示和清除一组按钮来编辑单词列表。 (有一个EditText用于添加,但这对于问题并不重要)。但是考虑到高内聚的想法,我已经定义了这个列表以及在另一个普通类中操作它的方法,这个类称为WordList(仍然扩展了Activity ),所以当试图调用按钮的onClick属性时,它找不到它们。

The idea of the program is to edit a list of words by adding to, displaying, and clearing it with a set of buttons. (There is an EditText for adding, but that's not important for the question). But with the idea of high cohesion in mind, I have defined this list and the methods that manipulate it in another plain class called WordList (which still extends Activity), and so when trying to invoke the onClick property of a button, it cannot find them.

android:onClick="addWord"




'MainActivity'中缺少方法'addWord'或签名错误...

Method 'addWord' is missing in 'MainActivity' or has incorrect signature...

有没有办法制作布局或单个元素点(或获取其数据上下文 )来自另一个类,还是反对Android的整个结构,我应该把它放在原来的活动中?

Is there a way to make the layout, or the individual element point (or get its data context) from another class, or is that against the whole structure of Android and I should just put it in its original activity?

推荐答案

您是否使用该方法的正确签名?

Are you using the correct signature for the method?

使用 onClick 属性的方法定义必须满足以下要求:

Methods defines using the onClick attribute must meet the following requirements:


  • 必须公开

  • 必须具有无效返回值

  • 必须将View对象作为参数(这是被点击的视图)

喜欢

public void addWord(View view) {
    //your action
}

这篇关于如何调用Button ellement的另一个activity / class onClick()中定义的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆