更改菜单选项卡时,在Android中改变选项卡、菜单、Android

2023-09-05 00:09:39 作者:资深情兽

我在我的应用程序两个标签,我想在菜单根据标签更改。

下面我做什么

  TabHost tabHost = tabHost = getTabHost();

    则tabspec photospec = tabHost.newTabSpec(照片);
    photospec.setIndicator(照片,getResources()getDrawable(R.drawable.photo)。);
    意图photosIntent =新的意图(这一点,Photos.class);
    photospec.setContent(photosIntent);



    则tabspec songspec = tabHost.newTabSpec(诗经);
    songspec.setIndicator(诗经,getResources()getDrawable(R.drawable.songs));
    意图songsIntent =新的意图(这一点,Songs.class);
    songspec.setContent(songsIntent);

    tabHost.addTab(photospec); //将照片添加标签
    tabHost.addTab(songspec); //将歌曲添加标签
 

现在当用户点击照片选项卡上,我想显示一个菜单,编辑图片,当用户点击歌曲标签我想显示控制歌曲的顺序的菜单。我想这每当用户点击任何标签做。

  @覆盖
 公共布尔onOptionsItemSelected(菜单项项){
    INT currentTab = tabHost.getCurrentTab();
    如果(currentTab == 0)
        startActivity(新意图(这一点,Photosoptions.class));
    如果(currentTab == 1)
        {
           startActivity(新意图(这一点,Songsoptions.class));
                      }
            返回true;
        }



      @覆盖
    公共布尔prepareOptionsMenu(功能菜单)在{

        MenuInflater充气= getMenuInflater();
        INT currentTab = tabHost.getCurrentTab();

        如果(currentTab == 0){
            menu.clear();
            inflater.inflate(R.menu.first,菜单);
           closeOptionsMenu();

           }
       如果(currentTab == 1){
            menu.clear();
            inflater.inflate(R.menu.second,菜单);
        closeOptionsMenu();

       }
        返回super.on prepareOptionsMenu(菜单);
    }
 
word设置代码专题及常见问题 CSDN

解决方案

您可以使用下面的code:

  @覆盖
 公共布尔prepareOptionsMenu(功能菜单)在{
        // TODO自动生成方法存根
        MenuInflater充气= getMenuInflater();
        INT currentTab = tabHost.getCurrentTab();
        Toast.makeText(getApplicationContext(),currentTab +,Toast.LENGTH_SHORT);
        menu.clear();
        如果(currentTab == 0){
            inflater.inflate(R.menu.first,菜单); //菜单photospec。
        } 其他 {
            inflater.inflate(R.menu.second,菜单); //菜单songspec
        }
        返回super.on prepareOptionsMenu(菜单);
    }
 

不需要你的 onCreateOptionsMenu ,然后你必须处理项目点击与 onOptionsItemSelected

I have two tabs in my application and I want the menu to change depending on the Tab.

Here what I did

  TabHost tabHost = tabHost = getTabHost();

    TabSpec photospec = tabHost.newTabSpec("Photos");
    photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.photo));
    Intent photosIntent = new Intent(this, Photos.class);
    photospec.setContent(photosIntent);



    TabSpec songspec = tabHost.newTabSpec("Songs");       
    songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.songs));
    Intent songsIntent = new Intent(this, Songs.class);
    songspec.setContent(songsIntent);

    tabHost.addTab(photospec); // Adding photos tab
    tabHost.addTab(songspec); // Adding songs tab

Now When the user clicks on the photos tab, I would like to display a menu for editing pictures and when the user clicks on songs tab I want to display a menu of controlling the order of the songs. I want to do this each time the user click on any of the tabs.

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    int currentTab = tabHost.getCurrentTab();
    if (currentTab == 0)
        startActivity(new Intent(this, Photosoptions.class));
    if (currentTab == 1)
        {
           startActivity(new Intent(this, Songsoptions.class));
                      } 
            return true;
        }



      @Override
    public boolean onPrepareOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        int currentTab = tabHost.getCurrentTab();

        if (currentTab == 0){
            menu.clear();
            inflater.inflate(R.menu.first, menu);
           closeOptionsMenu();

           }
       if (currentTab ==1){
            menu.clear();
            inflater.inflate(R.menu.second, menu);
        closeOptionsMenu();

       }
        return super.onPrepareOptionsMenu(menu);
    }

解决方案

you can use following code:

@Override
 public boolean onPrepareOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        MenuInflater inflater = getMenuInflater();
        int currentTab = tabHost.getCurrentTab();
        Toast.makeText(getApplicationContext(), currentTab+"", Toast.LENGTH_SHORT);
        menu.clear();
        if (currentTab == 0) {
            inflater.inflate(R.menu.first, menu);  //  menu for photospec.
        } else {
            inflater.inflate(R.menu.second, menu);  // menu for songspec
        }
        return super.onPrepareOptionsMenu(menu);
    }

you don't needed onCreateOptionsMenu and you must handle item click with onOptionsItemSelected