【问题标题】:display a context menu when clicking on tabBar单击 tabBar 时显示上下文菜单
【发布时间】:2012-06-26 18:01:44
【问题描述】:

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

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

希望我说清楚了。谢谢!

【问题讨论】:

标签: android android-activity tabs


【解决方案1】:

下面给出的简单演示示例,检查它是否根据您的要求进行一些更改

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");  
} 
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-21
    • 2020-01-08
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    相关资源
    最近更新 更多