Android,从代码调用视图对象
我是一个新手,所以如果这个问题太简单或太难,请原谅。
我的 java 文件中有这段代码:
public void button_baby_clicked(View v)
{
//do something here
}
当有人单击我的 xml 文件中的图像按钮时,会调用此代码,但如何从 java 文件本身调用此代码? 因为它需要一个 View 对象......我猜我需要重新创建它?如何?
编辑:
好的,澄清一下,我希望能够通过单击 xml 文件及其下的函数来调用上述函数。 例如:
public void button_baby_clicked(View v)
{
//do something here
}
public void someFunction()
{
x = 10;
button_baby_clicked(); // This should call the above function.
}
I am kind of a newbie so excuse me if this question is too simple or too hard.
I have this code in my java file:
public void button_baby_clicked(View v)
{
//do something here
}
this gets called when someone clicks the imagebutton in my xml file, but how do I call this from the java file itself?
Because it's expecting a View object... and I'm guessing I need to recreate that? How?
Edit:
Ok, to clarify, I want to be able to call the above function via a click in my xml file as well as a function under it.
For example:
public void button_baby_clicked(View v)
{
//do something here
}
public void someFunction()
{
x = 10;
button_baby_clicked(); // This should call the above function.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在您的 ImageButton 中,您必须添加一个属性:
android:onClick="button_baby_clicked"
在 java 文件中,您添加了:
其背后的逻辑是:
单击您的图像按钮后,将自动调用此方法,即“v”参数将具有您的图像按钮。
这样做的好处是:
您不需要在您的活动中初始化图像按钮,也不需要为此图像按钮设置点击侦听器。
In ur ImageButton you have to add an attribute:
android:onClick="button_baby_clicked"
In the java file, you have added:
The logic behind this is:
Upon clicking ur imagebutton, this method will automatically get called, i.e "v" argument will be having ur imagebutton.
The advantage of giving like this is:
You no need to initialize the imagebutton in ur activity and no need to set click listener too for this imagebutton.
好吧,如果您希望每次单击视图时都调用该方法,请按照其他人所说的操作。
或者,您可以执行类似的操作。
然后,每当您希望使用特定的
View
调用它时,只需调用button_baby_clicked(globalReference);
您也可以使用任何
View
执行此操作您动态创建的对象。只需获取与该方法相同作用域内的有效
View
引用,然后像任何其他方法一样将其传入。如果该方法能够处理它,它甚至可以为 null。Alright, if you want to have the method invoked every time the view is clicked, do what the others have said.
Alternatively, you can do something like this.
Then, whenever you want that to be called with that particular
View
, simply callbutton_baby_clicked(globalReference);
You can also do this with any
View
object you create dynamically.Just get a valid
View
reference within the same scope as the method, and pass it in like any other method. It can even be null if the method is capable of handling it.你不能像- 那样使用它吗
?
编辑:
如果您需要从按钮的onClick调用someFunction(),并从那里调用button_baby_clicked(),您必须在someFunction中获取View v对象。此链接可能对您有帮助。请参阅在onClick上启动服务。你可以适当改变。
Can't you use it like -
??
EDIT :
If you need to call someFunction() from the onClick of a button,and from there,you need to call button_baby_clicked(),you have to get View v object in someFunction. This link might help you. Please refer Start a service on onClick. You can change appropriately.
我相信最好重构代码并将事件处理程序中的代码放入可以从任何地方调用的全局方法中。像这样:
这样您就可以随时随地重用
taskToPerform()
方法中的代码。I believe its best if you refactor your code and put the code in the event handler into a global method that can be called from anywhere. like this:
This way you can reuse the code in the
taskToPerform()
method anywhere, anytime.