java - Android 中的简单选项卡 Activity

标签 java android android-tabhost android-tabactivity

我正在尝试做什么::

enter image description here

BreakfastLunchDinnerIndividualListOfItems.java

public class BreakfastLunchDinnerIndividualListOfItems extends TabActivity implements OnTabChangeListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.breakfast_lunch_dinner_individual_list_of_items);

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost(); // The activity TabHost
        TabHost.TabSpec spec; // Reusable TabSpec for each tab
        Intent intent; // Reusable Intent for each tab

        String REST = getTabHost().toString();

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, BLD_IndividualListOfItems_Starters.class);
        //intent.putExtra("Starters", REST);
        spec = tabHost.newTabSpec("Starters").setIndicator("Starters").setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs

        intent = new Intent().setClass(this, BLD_IndividualListOfItems_MainCourse.class);
        //intent.putExtra("MainCourse", REST);
        spec = tabHost.newTabSpec("MAIN_COURSE").setIndicator("Main Course").setContent(intent);
        tabHost.addTab(spec);
    }

    public void onTabChanged(String arg0)
    {
        // TODO Auto-generated method stub
        //Toast.makeText(getApplicationContext(),arg0, Toast.LENGTH_LONG).show();
    }
}
<小时/>
  • 我尝试使用 String REST = getTabHost().toString(); 问题是 单击选项卡时,我无法将特定的 tabtext 发送到 选项卡启动的 Activity
  • 我知道标签页 Activity 已被取消,我只是在学习
  • 如何解决这个问题,希望我很清楚

最佳答案

spec = tabHost.newTabSpec("Starters").setIndicator("Starters").setContent(intent);
spec = tabHost.newTabSpec("MAIN_COURSE").setIndicator("Main Course").setContent(intent);

这里的StartersMain Course是选项卡的标题。将此字符串发送到有界 Activity 的最简单方法是通过绑定(bind)到相应 Activity 的 Intent 发送它们 选项卡规范。

    String TAB_TITLE="Starters";
    Intent intent = new Intent().setClass(this, BLD_IndividualListOfItems_Starters.class);

    Bundle bundle =new Bundle();
    bundle.putString("key_title", TAB_TITLE);
    intent.putExtras(bundle);

    spec = tabHost.newTabSpec("Starters").setIndicator(TAB_TITLE).setContent(intent);
    tabHost.addTab(spec);

以下是如何在 BLD_IndividualListOfItems_Starters Activity 中获取此字符串:

protected void onCreate(Bundle savedInstanceState) {
     ...
     String title=getIntent().getExtras().getString("key_title");
}

但正如 Raghunandan 所说,最好使用 fragment 而不是废弃的 TabActivity

编辑:

如果您想动态地将文本发送到相应的 Activity ,我的意思是在选项卡更改后 - 您可以:

  1. 使用您的字符串创建并广播自定义 Intent 。 (CUSTOM_INTENT_EXAMPLE)
  2. BLD_IndividualListOfItems_Starters Activity 中注册 BroadcastReciever ,它将捕获您的自定义 Intent 并从中获取文本。 (BROADCAST_RECIEVER_FROM_ACTIVITY_EXAMPLE)

您无法直接从 BLD_IndividualListOfItems_Starters 访问选项卡文本,因为 TabActivityBLD_IndividualListOfItems_Starters 是两个不同的 Activity。 但是您可以通过 bundle 、静态字段、单例等在 Activity 之间发送数据。这里是文档链接 http://developer.android.com/guide/faq/framework.html#3

关于java - Android 中的简单选项卡 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20834434/

相关文章:

java - 强制 maven spring boot 项目使用旧版本的依赖项,而不是另一个依赖项的新版本

android - React-Native-Firebase Cloud Messaging-如果从任务栏打开则没有事件

android - 在 android eclipse 上处理 tabhost

android - 如何克服android中tabwidget的这种情况

java - 在使用 AbstractRoutingDataSource 切换数据源时共享事务

java - 如何创建 android 库并作为单个 jar 进行分发,无需资源和 android list 文件

java - 如何实现带有进度条的闪屏? - 安卓

java - SharedPreferences - 静态类中的静态字符串?

android - 在 Android SQLite 中检索两个日期之间的数据

android - 按下后退按钮