2017-01-03 157 views
2

在我的活動,我有兩個圖標按鈕,在action bar如何獲得2個選項選項?

enter image description here

這是add_task.xml

<item 
    android:id="@+id/action_add_task" 
    android:icon="@drawable/create_new" 
    android:title="Add New" 
    app:showAsAction="always" /> 

<item 
    android:id="@+id/menu" 
    android:icon="@drawable/menu" 
    android:title="Menu" 
    app:showAsAction="always" /> 

當點擊該圖標menu,我得到這個

enter image description here

popup.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
    android:id="@+id/opt1" 
    android:icon="@drawable/change_pic" 
    android:title="Change Profile" /> 

    <item 
     android:id="@+id/opt2" 
     android:icon="@drawable/sign_out" 
     android:title="Sign Out" /> 
</menu> 

我的問題是,當menu圖標點擊Toast得到顯示。我只希望它在點擊更改配置文件時顯示。

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.add_task, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 

     case R.id.menu: 
       View menuItemView = findViewById(R.id.menu); 
       MenuBuilder menuBuilder =new MenuBuilder(this); 
       MenuInflater inflater = new MenuInflater(this); 
       inflater.inflate(R.menu.popup, menuBuilder); 
       MenuPopupHelper optionsMenu = new MenuPopupHelper(this, menuBuilder, menuItemView); 
       optionsMenu.setForceShowIcon(true); 
       optionsMenu.show(); 

       case R.id.opt1: // when change profile clicked 
      Toast.makeText(getApplication(),"Profile",Toast.LENGTH_SHORT).show(); 


      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

編輯

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     switch (item.getItemId()) { 

      case R.id.menu: 
       View menuItemView = findViewById(R.id.menu); 
       MenuBuilder menuBuilder = new MenuBuilder(this); 
       MenuInflater inflater = new MenuInflater(this); 
       inflater.inflate(R.menu.popup, menuBuilder); 
       final MenuPopupHelper optionsMenu = new MenuPopupHelper(this, menuBuilder, menuItemView); 
       opionsMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 
        public boolean onMenuItemClick(MenuItem item) { 
         // Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show(); 
         if ("Change Profile".equals(item.getTitle())) { 
          Intent intent = new Intent(AddMonthlyExpenses.this, Profile.class); // go to Information class 
          intent.putExtra("name", name); 
          startActivity(intent); 

         } 
         optionsMenu.setForceShowIcon(true); 
         optionsMenu.show(); 
         return true; 
        } 
       }); 

      default: 
       return super.onOptionsItemSelected(item); 

     } 

     return true; 
    } 

我就在這行

opionsMenu.setOnMenuItemClickListener 

optionsMenu不能得到解決的錯誤。

+0

'opionsMenu'!='optionsMenu' ..there是一個錯字您的代碼.. – n0rph3u5

+0

如果add_task在菜單文件夾中,那麼根標籤必須是* menu * –

回答

0

最後我找到了答案

  case R.id.menu: 
      View menuItemView = findViewById(R.id.menu); 
      MenuBuilder menuBuilder = new MenuBuilder(this); 
      MenuInflater inflater = new MenuInflater(this); 
      inflater.inflate(R.menu.popup, menuBuilder); 
      MenuPopupHelper optionsMenu = new MenuPopupHelper(this, menuBuilder, menuItemView); 
      optionsMenu.setForceShowIcon(true); 

      // Set Item Click Listener 
      menuBuilder.setCallback(new MenuBuilder.Callback() { 
       @Override 
       public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) { 
        switch (item.getItemId()) { 
         case R.id.opt1: // Handle option1 Click 
          Intent intent = new Intent(AddMonthlyExpenses.this,Profile.class); 
          startActivity(intent); 
          return true; 
         case R.id.opt2: // Handle option2 Click 
          return true; 
         default: 
          return false; 
        } 
       } 

       @Override 
       public void onMenuModeChange(MenuBuilder menu) {} 
      }); 


      // Display the menu 
      optionsMenu.show(); 
0

你還沒有加入Break語句

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

    super.onOptionsItemSelected(item); 

    switch(item.getItemId()){ 
     case R.id.menu: 
       View menuItemView = findViewById(R.id.menu); 
       MenuBuilder menuBuilder =new MenuBuilder(this); 
       MenuInflater inflater = new MenuInflater(this); 
       inflater.inflate(R.menu.popup, menuBuilder); 
       MenuPopupHelper optionsMenu = new MenuPopupHelper(this, menuBuilder, menuItemView); 
       optionsMenu.setForceShowIcon(true); 
       optionsMenu.show(); 
       break; 

    case R.id.opt1: // when change profile clicked 
      Toast.makeText(getApplication(),"Profile",Toast.LENGTH_SHORT).show(); 
       break; 

    } 
    return super.onOptionsItemSelected(item); 

} 

試試這個代碼

+0

我將會丟失返回語句 – Tony

+0

嘗試移除默認值並查看else在所有情況下添加return語句 – Redman

+0

現在,我單擊更改配置文件,不顯示Toast display – Tony