全球及QUOT;搜索功能"在整个应用程序 [英] Global "search function" in whole app

查看:94
本文介绍了全球及QUOT;搜索功能"在整个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序在整个我想搜索按钮进行单独的活动。即从应用程序的任何地方,当我preSS搜索按钮,我希望有一个单独的活动来调用。

In my application throughout I want the search button to perform a separate Activity. i.e. From anywhere in the app when I press the search button I want a separate Activity to get called.

有,而不是每一个活动定义 onSearchRequested(),我只是将其配置在一个地方(如的Manifest.xml <任何方式/ code>),它可以在整个应用程序中使用?

Is there any way that instead of defining onSearchRequested() in every activity, I just configure it at one place (like Manifest.xml) and it can be used throughout the app?

推荐答案

您可以定义扩展活动的(不一定)抽象类,实现onSearchRequest那里继承了所有其他活动课从该类。通过这种方式,你只需要定义只有一次onSearch行为。

You could define an (not necessarily) abstract class that extends Activity, implement onSearchRequest there and inherit all other Activity classes from that class. In this way you only have to define the onSearch behaviour only once.

public abstract class MyBaseActivity extends Activity {
    @Override
    public void onSearchRequest() {
       // Your stuff here
    }
}

public class MyActivity1 extends MyBaseActivity {
   // OnSearchRequest is already implemented
}

如果您计划使用活动的多个子类,即ListActivity

,这可能不是一个很好的解决方案,因为你必须创建一个抽象基类为您使用所有活动的子类。在这种情况下,我建议你创建一个额外的类,封装搜索按钮操作code和调用,从你的活动onSearchRequest,即

If you plan to use multiple subclasses of Activity, i.e. ListActivity, this might not be a good solution, as you have to create a abstract base class for all Activity subclasses you use. In this case I'd recommend creating an additional class, encapsulating the search button handling code and call that from you activities onSearchRequest, i.e.

public class SearchButtonHandle {
    public void handleSearch(Context c) {
       // Your search btn handling code goes here
    }  
}

public class MyActivity1 extends Activity {  // Or ListActivity ....
    @Override
    public void onSearchRequest() {
       new SearchButtonHandle().handleSearch(this);
    }
}

当然,你也可以通过定义一个抽象类,你使用所有活动的子类的同时结合了作者:刘美平和实施onSearchRequest如上面与外部搜索处理器

Of course you can also combine both approches by defining an Abstract Subclass of all Activity Subclasses you use and implement the onSearchRequest as in the example above with an external Search Handler

这篇关于全球及QUOT;搜索功能&QUOT;在整个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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