2011-07-04 18 views
2

我100%確定這將是其中一個新手問題,但這裏它去...通過應用程序可用的方法?

有沒有一種方法,我可以在一個活動中寫一個方法,並能夠從其他?

例子: 我在我的應用程序6個activites,每個都有它自己的menu.xml文件,因爲可用於每個選項需要是不同的,我有這些菜單&的菜單項設置,如下所示:

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.calculator_menu, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     //Handle item selection 
     switch (item.getItemId()) { 
     case R.id.menuItem_calculator_Help: 
      helpDialogGo(); 
      return true; 
     case R.id.menuItem_calculator_Settings: 
      //settingsActivityGo(); 
      return true; 
     case R.id.menuItem_calculator_Share: 
      shareGo(); 
      return true; 
     case android.R.id.home: 
      // app icon in Action Bar clicked; go home 
      Intent uptohome = new Intent(this, Main.class); 
      uptohome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(uptohome); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
     } 
    } 

所述的這些方法之一的例子是:

private void helpDialogGo() { 
     Toast.makeText(this, "help", Toast.LENGTH_LONG).show(); 
     AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); 
     alt_bld.setMessage("Sorry, no help has been written since this application is still in development. This is a prerelease version.") 
     .setCancelable(false) 
     .setPositiveButton("Cool", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
     // Action for 'Yes' Button 
     dialog.cancel(); 
     } 
     }) 
     .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
     // Action for 'NO' Button 
     dialog.cancel(); 
     } 
     }); 
     AlertDialog alert = alt_bld.create(); 
     // Title for AlertDialog 
     alert.setTitle("Pixel Help"); 
     // Icon for AlertDialog 
     alert.setIcon(R.drawable.question); 
     alert.show(); 
    } 

所以是有辦法知道所有的活動間共享這個自定義的方法,並且當在它們中的每按下按鈕運行它,以避免有大量的金錢在我的應用程序中複製代碼?

如果是的話,是沒有辦法,我可以打任何坑窪? (某些菜單項會彈出對話框,其他人將用戶帶到一個新的活動)

回答

2

您是否在每項活動中都有類似的菜單項?即相同數量的項目但行爲不同?如果是... 如何創建一個覆蓋onCreateOptionsMenu和onOptionsItemSelected()方法的BaseActivity ..(正如您在上面的例子中給出的那樣)。您的所有活動都應該從此BaseActivity繼承,然後重寫菜單處理方法。例如。 helpDialogGo()將轉到新的類。

所以BaseActivity將有onCreateOptionsMenu和onOptionsItemSelected()方法。加上所有的menuItem動作(即helpDialogGo()等)作爲空方法。繼承的類將覆蓋menuItem操作。

如果的菜單項不是每個活動類似,您最好爲每個活動創建菜單。

編輯:

不知道你期待更多。我以爲我說得很清楚。讓我再嘗試一次。

BaseActivity延伸Activity

BaseActivity extends Activity { 

    // Copy your onCreateOptionsMenu() and onOptionsItemSelected() methods here 

    protected void helpDialogGo() { } 

    // ... other methods 
} 

MyActivity1延伸BaseActivity

MyActivity1 extends BaseActivity { 

    // Copy your helpDialogGo() code in full here and then make 
    // any specific changes to menu behaviour based on activity. 

} 

MyActivity2延伸BaseActivity

MyActivity2 extends BaseActivity { 
    // Copy your helpDialogGo() code in full here and then make 
    // any specific changes to menu behaviour based on activity. 
} 
+0

這實際上是相當常見的做法。這不是特定於Android的內容,而是與類設計的最佳實踐更相關。 – omermuhammed

+0

好的。在我開始之前,你能詳細闡述一下嗎? – rabbitt

+0

@omermuhammed同意。 – GSree

1

的一種方式,當然是要創建封裝所需功能的一些自定義類 - 並在使用那些你活動。將實現直接放置在Activity(s)本身(所有的東西都是平等的,並且基於你迄今爲止所描述的),這是一個更好的抽象。

任何時候,你發現自己複製的implmentation這就是提醒你這是把自己的代碼到自己的類的好地方的標誌 - 通常。

+0

好了,所以它_is_一個好主意嗎?現在我該怎麼辦?一個新的類,每個方法都在其中,還是全部在其中?我知道要問很多,但示例代碼會很好。謝謝你的幫助! – rabbitt

相關問題