2016-11-08 43 views
0

我有一個片段與一個單一的Button。我的目標是每當用戶點擊該按鈕時顯示menu_language.xml。我試圖找到一個在線答案,這是最接近我可以得到它...但它不工作:如果我按下按鈕沒有任何反應。這段代碼有什麼問題?點擊後顯示菜單按鈕:爲什麼我的代碼不工作?

@Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View fgm = inflater.inflate(R.layout.fragment_settings, container, false); 
     Button changeLanguageBtn = (Button) fgm.findViewById(R.id.change_language_button); 
     changeLanguageBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       getActivity().openOptionsMenu(); 
      } 
     }); 
     return fgm; 
    } 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) { 
     super.onCreateContextMenu(menu, view, menuInfo); 
     if (view.getId() == R.id.change_language_button) { // is this even necessary? 
      MenuInflater inflater = getActivity().getMenuInflater(); 
      inflater.inflate(R.menu.menu_language, menu); 
     } 
    } 

    @Override 
    public boolean onContextItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.action_english: 
       // switch to English 
       return true; 
      case R.id.action_italian: 
       // switch to Italian 
       return true; 
      case R.id.action_french: 
       // switch to French 
       return true; 
      default: 
       return super.onContextItemSelected(item); 
     } 
    } 

這是菜單的XML:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 

    <group android:checkableBehavior="single"> 
     <item 
      android:id="@+id/action_italian" 
      android:title="@string/italian" /> 
     <item 
      android:id="@+id/action_english" 
      android:title="@string/english" /> 
     <item 
      android:id="@+id/action_french" 
      android:title="@string/french" /> 
    </group> 
</menu> 
+0

你在使用工具欄嗎? –

+0

有一個工具欄,但'change_language_button'是**不存在**:它位於屏幕中間。 – Robb1

+0

我想你實現了錯誤的方法,onCreateOptionsMenu vs onCreateContextMenu –

回答

0

我覺得你有錯誤的方法(如果你想添加到ActionBar的菜單:如果你讀這個方法的Javadoc

public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) 

​​

而:

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

說:

/** 
    * Initialize the contents of the Activity's standard options menu. You 
    * should place your menu items in to <var>menu</var>. For this method 
    * to be called, you must have first called {@link #setHasOptionsMenu}. See 
    * {@link Activity#onCreateOptionsMenu(Menu) Activity.onCreateOptionsMenu} 
    * for more information. 
    * 
    * @param menu The options menu in which you place your items. 
    * 
    * @see #setHasOptionsMenu 
    * @see #onPrepareOptionsMenu 
    * @see #onOptionsItemSelected 
    */ 

所以,如果你想菜單添加到活動中,你必須以後使用。現在,如果您想顯示一個上下文菜單錨定到您按下的按鈕,然後查看PopupMenu類。

關於你的is this needed?評論,答案是最有可能沒有你的情況。

+0

感謝您的回答。我試圖按照你的建議使用**選項菜單**,但是我無法使它工作。你介意直接修復我在這種情況下應該使用的代碼嗎? – Robb1

+0

我有點忙,atm,我沒有在一段時間內使用菜單,但從我記得,你會想實現'公共布爾onPrepareOptionsMenu(最終菜單菜單){'當按鈕按時,你可以設置一個flag /布爾值來指示是否要顯示(或隱藏)菜單。 onPrepareOptionsMenu將決定(因爲它正在接收菜單)添加/刪除項目到該菜單(如果你願意,可以從XML膨脹)。試一試,讓我們知道它是如何去的。搜索:「在運行時android以編程方式修改菜單」或類似內容,您可能會發現很多樣本。祝你好運! –

+0

另外我_think_你需要調用'invalidateOptionsMenu();'所以調用回調(但我不記得atm) –

相關問題