2017-07-27 27 views
4

例如,如何添加我經常用於android studio的方法?

public void show_message(String message){ 
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); 
} 

我想這種方法時創建新的活動或Java類添加自動Activity.java。

我想保存不同的方法是這樣的,包括它在我的項目迅速地在需要的地方。

+0

你可以做一個自定義類與自定義方法和在 –

+0

它不會創造每一個活動生成的每個項目的導入。但是你可以做的是創建一個名爲Base Activity的Activity並在那裏實現所有這些方法。然後,您創建的每個新活動都可以通過BaseActivity擴展。然後您可以在所有活動中訪問它 –

+0

您可以創建自定義註釋,並用該註釋對類進行註釋。這很難,因爲註釋必須寫給班級。您可以通過查看[Lombok源代碼](https://github.com/rzwitserloot/lombok)並查看getter/setter註釋來檢查寫入該類的註釋示例。它將方法寫入一個類。 – Zoe

回答

3

你只需要做出常見的實用工具類。只需將該類複製並粘貼到您正在使用的任何項目中即可。只需將其方法訪問說明符設爲public staic,以便您可以輕鬆訪問它。 例如

CommonUtilities.showToastMessage(String text); 
7

你應該做的是創建一個BaseActivity並讓你的活動擴展這個BaseActivity。添加此活動中的所有默認方法,以便您可以在任何地方使用它們。您可以參考this Github項目以供參考。它使用MVP。

這裏是直接鏈接到BaseActivity

0

我會做的是創建一個配置類,並將所有這些小東西存儲在其中。例如看看這個:

public class Config { 
    public Context context; 
    public String sharedPrefsName; 
    public String carTablesName, carsTableCarColumn, databaseName; 
    public int databaseNewVersion, databaseOldVersion; 
    public boolean showNotificationsToCustomer; 
    public String customerNotificationState; 
    public String userMobile; 
    public SharedPreferences preferences; 
    public String customerChatTableName; 
    public String customerChatMessageColumn; 
    public String customerChatSentByCustomerColumn; 
    public String customerChatTimeColumn; 
    public String loggedInUserId; 
    public String loggedInUserName; 
    public String customerChatSupportNotifyingUrl; 

    public Config(Context context) { 
     this.context = context; 
     customerChatSupportNotifyingUrl = ""; 
     customerChatTableName = "customerChat"; 
     customerChatMessageColumn = "customerMessage"; 
     customerChatTimeColumn = "sentOn"; 
     customerChatSentByCustomerColumn = "isSentByCustomer"; 
     sharedPrefsName = context.getString(R.string.shared_prefs_login_validator); 
     preferences = context.getSharedPreferences(sharedPrefsName, Context.MODE_PRIVATE); 
     customerNotificationState = context.getString(R.string.customer_notification_state); 
     showNotificationsToCustomer = preferences.getBoolean(customerNotificationState, true); 
     carTablesName = context.getString(R.string.user_car_table); 
     carsTableCarColumn = context.getString(R.string.user_car_table_car_column); 
     databaseName = context.getString(R.string.user_db); 
     databaseNewVersion = 3; 
     databaseOldVersion = 1; 
     loggedInUserId = preferences.getString(context.getString(R.string.user_db), ""); 
     userMobile = preferences.getString(context.getString(R.string.user_mobile), ""); 
     loggedInUserName = preferences.getString(context.getString(R.string.user_name), ""); 
    } 
} 

我已經把所有的常量放在一個單獨的文件中,所以你不必總是看着它們。如果你的應用程序的規模增長,這將是非常有用的。

對於使用進度對話框我用一類這樣的:

public class MyProgressDialog extends ProgressDialog { 
    String title, message; 

    public MyProgressDialog(Context context, String title, String message) { 
     super(context); 
     if (!title.equals("")) this.setTitle(title); 
     this.setMessage(message); 
     this.setCancelable(false); 
     this.setIndeterminate(false); 
    } 
} 

這無非是擴展ProgressDialog.So可以AQUIRE進度對話框類的所有功能的單一類。

類似的烤麪包,你可以做同樣的事情。如果您希望它們在創建活動時出現,請將其保存爲:

MyProgressDialog dialog=new MyProgressDialog(this,"title","message"); 
dialog.show(); 

您的活動的onCreate()方法。你也可以爲烤麪包做同樣的事情。

在情況下,如果它是一個Java類,只需創建一個構造函數,並保持該片段在構造函數中..

相關問題