单击tabBar时显示上下文菜单

时间:2012-06-26 18:01:44

标签: android android-activity tabs

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

重要因此,当我点击第三个标签时,我不能转到另一个活动,我必须保持当前活动(来自标签2)并显示上下文菜单。

希望我明白自己。谢谢!

1 个答案:

答案 0 :(得分: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");  
} 
}