android 如何在启动其他活动时保留Tabhost

jmp7cifd  于 2022-12-25  发布在  Android
关注(0)|答案(3)|浏览(129)

我的TabHost有问题...
我的TabBarActivity类流为:
公共类TabBarActivity扩展TabActivity实现OnTabChangeListener{

/*
 * (non-Javadoc)
 * 
 * @see android.app.ActivityGroup#onCreate(android.os.Bundle)
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.layout_tab);

    TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);

    TabSpec mTab1 = tabHost.newTabSpec("tab1");
    TabSpec mTab2 = tabHost.newTabSpec("tab2");

    mTab1.setIndicator("TAB1").setContent(
            new Intent(this, TAB1.class));
    mTab2.setIndicator("TAB2").setContent(
            new Intent(this, TAB2.class));

    /** Add tabSpec to the TabHost to display. */
    tabHost.addTab(mTab1);
    tabHost.addTab(mTab2);

}

而且我在www.example.com类中有ListView(例如联系人姓名TAB1.java
假设:我有一个屏幕布局,底部有两个选项卡(TAB 1,TAB 2)。当我单击TAB 1时,我有一个联系人姓名列表,如果我单击itemList。将打开itemList的详细信息屏幕。在“DetailScreen”布局中,我希望将(TAB 1,TAB 2)选项卡主机保留在屏幕底部。
现在,当我运行一个应用程序并点击itemList时,我没有在每个项目列表的DetailScreen中保留Tabhost(TAB 1,TAB 2)...
如何在我启动项目列表的Activity时保留它(例如:DetailScreen.java
谢谢
@ all:谢谢你的反馈,但是看起来你并不理解我的问题。
我的问题:详细说明我的项目:1-我有一些类:+TabBarActivity.java(源代码流程如上所述,顶部页面,底部布局有2个选项卡,选项卡1、选项卡2......。运行应用程序时默认聚焦选项卡1)+Tab1.java(重要:在这门课上,我有listview(比如电话上的youtube),当我点击列表上的一个项目-〉打开列表中每个项目的详细信息屏幕...确定)+Tab2.java(做点什么...)
2 -当我运行应用程序,你看到2标签底部布局和默认startActivity Tab1.java-〉然后在此屏幕中显示列表视图...当单击列表中的一个项目-〉打开此项目的详细信息屏幕(StartActivity DetailItem.java)-〉但2标签底部将消失....
我不知道为什么????
请帮帮我:((

dy1byipe

dy1byipe1#

必须使用TabGroupActivity类,
此外,您还必须实现从TabGroupActivity扩展而来的Tab1Activity类和Tab2Activity类
在这里您将分别调用startChildActivity(TAB1.class)和startChildActivity(TAB2.class),这样您就可以选择您的选项卡是位于TAB1还是TAB2中。
你可以在线实现TabGroupActivity类的样例
最近,我有类似的项目,是Y米这样陈述。

5uzkadbs

5uzkadbs2#

您可以使用以下自定义类来实现此目的:

import java.util.ArrayList;

import android.app.Activity;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;

/*
 * The purpose of this Activity is to manage the activities in a tab.
 * Note: Child Activities can handle Key Presses before they are seen here.
 * @author Eric Harlow
 */
public class TabGroupActivity extends ActivityGroup {

    private ArrayList<String> mIdList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);       
        if (mIdList == null) mIdList = new ArrayList<String>();
    }

    /*
     * This is called when a child activity of this one calls its finish method. 
     * This implementation calls {@link LocalActivityManager#destroyActivity} on the child activity
     * and starts the previous activity.
     * If the last child activity just called finish(),this activity (the parent),
     * calls finish to finish the entire group.
     */
  @Override
  public void finishFromChild(Activity child) {
      LocalActivityManager manager = getLocalActivityManager();
      int index = mIdList.size()-1;

      if (index < 1) {
              finish();
              return;
          }

          manager.destroyActivity(mIdList.get(index), true);
          mIdList.remove(index);
          index--;
          String lastId = mIdList.get(index);
          Intent lastIntent = manager.getActivity(lastId).getIntent();
          Window newWindow = manager.startActivity(lastId, lastIntent);
          setContentView(newWindow.getDecorView());
  }

  /*
   * Starts an Activity as a child Activity to this.
   * @param Id Unique identifier of the activity to be started.
   * @param intent The Intent describing the activity to be started.
   * @throws android.content.ActivityNotFoundException.
   */
  public void startChildActivity(String Id, Intent intent) {     
      Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
      if (window != null) {
          mIdList.add(Id);
          setContentView(window.getDecorView()); 
      }    
  }

  /*
   * The primary purpose is to prevent systems before android.os.Build.VERSION_CODES.ECLAIR
   * from calling their default KeyEvent.KEYCODE_BACK during onKeyDown.
   */
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_BACK) {
          //preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
          return true;
      }
      return super.onKeyDown(keyCode, event);
  }

  /*
   * Overrides the default implementation for KeyEvent.KEYCODE_BACK 
   * so that all systems call onBackPressed().
   */
  @Override
  public boolean onKeyUp(int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_BACK) {
          onBackPressed();
          return true;
      }
      return super.onKeyUp(keyCode, event);
  }

  /*
   * If a Child Activity handles KeyEvent.KEYCODE_BACK.
   * Simply override and add this method.
   */
  @Override
  public void  onBackPressed  () {
      int length = mIdList.size();
      if ( length >=1) {
          Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
          current.finish();
      }
  }
}

像这样使用:
在包含选项卡的主类中:

public class MainActivity extends TabGroupActivity {

}

在onItemClickListener中,您可以像这样启动活动:

startChildActivity("CollectionList", new Intent(this,CollectionMenu.class));

当你在CollectionMenu(扩展TabGroupActivity)中时,你可以像下面的代码一样启动你的子Activity:

Intent previewMessage = new Intent(getParent(), DetailScreen.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("DetailScreen", previewMessage);

这个应该可以。如果你有任何问题尽管问!

gwo2fgha

gwo2fgha3#

使用方法:
在一些标签页中的一个标签页中,例如4个标签页,每个标签页都扩展TabActivity,在其中您使用方法startChildActivity开始您的实际活动。
代码

public class Tab_BookCityActivity extends TabGroupActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    startChildActivity("BookCityActivity", new Intent(this, BookCityActivity.class));

    }
}

相关问题