1

我有一个奇怪的要求要完成。假设我有一个带有 3 个标签的 tabHost。假设第二个选项卡当前显示。我需要做的是 - 单击第三个选项卡以显示上下文菜单时。上下文菜单需要显示在选项卡 2 的活动中。

重要因此,当我单击第三个选项卡时,我不能转到另一个活动,我必须留在当前活动(来自选项卡 2)并还显示上下文菜单。

希望我说清楚了。谢谢!

4

1 回答 1

0

如下所示的简单演示示例,检查它根据您的要求进行一些更改

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

public class Tab_exActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Resources res = getResources(); // Resource object to get Drawables
    // this.myhost = (TabHost)this.findViewById(R.);

    // this.myhost.setup();
     final TabHost tabHost=getTabHost(); 
     // The activity TabHost
     TabHost.TabSpec spec;  // Resusable TabSpec for each tab
     Intent intent;  // Reusable Intent for each tab

     // Create an Intent to launch an Activity for the tab (to be reused)
     intent = new Intent().setClass(this, tabactivity1.class);

     // Initialize a TabSpec for each tab and add it to the TabHost
     spec = tabHost.newTabSpec("artists").setIndicator("Home",
                       res.getDrawable(R.drawable.act1))
                   .setContent(intent);
     tabHost.addTab(spec);

     // Do the same for the other tabs
     intent = new Intent().setClass(this, tabactivity2.class);
     spec = tabHost.newTabSpec("albums").setIndicator("Refresh",
                       res.getDrawable(R.drawable.act2))
                   .setContent(intent);
     tabHost.addTab(spec);

    // intent = new Intent().setClass(this, tabactivity3.class);
     spec = tabHost.newTabSpec("songs").setIndicator("Search",
                       res.getDrawable(R.drawable.act3))
                   .setContent(intent);

     tabHost.addTab(spec);
     tabHost.setOnTabChangedListener(new OnTabChangeListener(){

         public void onTabChanged(String tabId) {

         if (tabId.equals("songs")){
             System.out.println("44444");
             View v = tabHost.getCurrentView();
             registerForContextMenu(v);
             v.showContextMenu();
         }

         }});


    // openContextMenu(spec);
    // tabHost.setCurrentTab(2);
}
@Override  
   public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
super.onCreateContextMenu(menu, v, menuInfo);  
    menu.setHeaderTitle("Context Menu");  
    menu.add(0, v.getId(), 0, "Action 1");  
    menu.add(0, v.getId(), 0, "Action 2");  
} 
} 
于 2012-06-27T07:34:27.593 回答