从另一个活动的片段中定义的活动中调用方法 [英] Calling a method from a activity which is defined in fragment of another activity

查看:65
本文介绍了从另一个活动的片段中定义的活动中调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为SearchActivity.java的actvity,它有一个方法dosearch()。



有一个名为fragment.java的片段类,它从MainActivity.java类调用



我可以在fragment.java中调用dosearch()方法吗?



如果可能的话怎么打电话?



我想点击fragment.java类中的按钮调用这个dosearch()方法。



我尝试了什么:



i尝试从片段类中的活动调用方法另一个活动..但是没有用?

Suppose i have a actvity named "SearchActivity.java" which has a method dosearch().

there is fragment class named "fragment.java" and it is called from "MainActivity.java" class

can i call dosearch() method in fragment.java?

if possible how to call?

and i want to call this dosearch() method on click of a button in fragment.java class.

What I have tried:

i tried calling a method from a activity in fragment class of another activity..but did not work?

推荐答案

为了调用 dosearch()方法在片段类中,你需要制作 dosearch()方法 static ,或者你需要一个 SearchActivity 对象。
In order to call the dosearch() method from within the fragment class, you'd either need to make the dosearch() method static, or you would need a SearchActivity object.


这篇关于从另一个活动的片段中定义的活动中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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