2012-02-03 50 views
7

在Android開發網站,我看到的onOptionsItemSelected的解釋,在return方面,它說: boolean Return false to allow normal menu processing to proceed, true to consume it here.任何人都可以說明「onOptionsItemSelected」的返回嗎?

對不起,我的愚蠢,任何人都可以說明什麼的句子解釋,我應該返回false在正常情況下?

+1

的可能的複製[應 「機器人:onOptionsItemSelected」 返回true或false(http://stackoverflow.com/questions/5719287/should-android-onoptionsitemselected-return-true-or-false) – 2015-11-12 20:19:03

回答

14

如果處理菜單項並返回super.onOptionsItemSelected(item),則返回true,否則返回true。

例如

public boolean onOptionsItemSelected(MenuItem item) 
{ 
    switch (item.getItemId()) 
    { 
     case R.id.option1: 
      handleOption1(); 
      return true; 
     case R.id.option2: 
      handleOption2(); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 
1

我覺得你可以讓系統做處理:

return super(...); 

否則,返回TRUE/FALSE只是意味着,如果你是治療的情況下,充分處理該事件只是返回TRUE。如果不是這種情況,那麼返回false,並且系統應該調度even到正確的處理程序。

相關問題