2012-06-26 57 views
0

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

重要所以,当我点击第三个选项卡我不能去到另一个活动上,我必须留在当前的活动(从标签2)并且还显示上下文菜单。

希望我说清楚了。谢谢!

+0

检查答案,因为在给定http://stackoverflow.com/questions/11204729/display-a-contextmenu-when -clicking-A-制表内式相同屏幕-与最突出部/ 11208500#11208500 – Khan

回答

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"); 
} 
} 
相关问题