如何在 Android 选项卡之间进行通信 [英] How to communicate between Android tabs

查看:17
本文介绍了如何在 Android 选项卡之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的 android 应用程序设置一些选项卡,但我卡住了.

Im trying to setup some tabs for my android application, but i got stuck.

我找不到在选项卡之间进行通信的方法..

I cant find a way to communicate between the tabs..

我有 2 个标签.

搜索选项卡只是显示一个文本编辑和一个搜索"按钮.点击搜索按钮应该会使我的应用更改为结果选项卡并显示结果.

The search tab is simply showing a TextEdit and a "Search" button. Hitting the search button should make my app change to the result tab and display the result.

我已将选项卡添加为活动,使用新意图作为

i have added the tabs as activities using new intents as

TabHost host=getTabHost();  

host.addTab(host.newTabSpec("one")  
   .setIndicator("Search")  
   .setContent(new Intent(this, SearchTabActivity.class)));  
host.addTab(host.newTabSpec("Result")  
   .setIndicator("Android")  
   .setContent(new Intent(this, ResultTabActivity.class)));  

现在我找不到 SearchTabActivity 显示 ResultTabActivity 并更改其视图的方法...

Now i cant find a way for SearchTabActivity to display ResultTabActivity and alter its view...

我期待任何提示.

推荐答案

肯定想重新考虑使用活动作为选项卡的内容.更标准的方法是使用一个使用选项卡的 Activity,仅在选择特定选项卡时显示部分布局.

You definitely want to reconsider using Activities as the content of your tabs. The more standard approach is to use one Activity that uses Tabs to only show part of the layout when a particular tab is selected.

Android 文档有一个很好的示例,请查看 你好,TabWidget.

The Android documentation has an excellent worked example, check out Hello, TabWidget.

替代方案

如果出于某种原因您确实需要使用活动,您可以通过将值添加到用于打开每个活动的 Intent 中的附加包,或通过扩展 Application 类.

If for some reason you do need to use Activities, you can pass information between them by either adding values to the extras bundle within the Intent your using to open each Activity, or by extending the Application class.

通过扩展 Application 类(并将其实现为 Singleton),您将获得一个在任何应用程序组件存在时都会存在的对象,从而提供一个集中的位置来存储和在应用程序组件之间传输复杂的对象数据.

By extending the Application class (and implementing it as a Singleton) you get an object that will exist whenever any of your application components exist, providing a centralized place to store and transfer complex object data between application components.

这篇关于如何在 Android 选项卡之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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