![](/img/trans.png)
[英]I can't use methods from another class from imported libraries in Android Studio
[英]How can I add methods that I often use to android studio?
例如,
public void show_message(String message){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
我希望這個方法在創建新活動或java類時添加自動Activity.java。
我想保存這樣的不同方法,並將其快速地包含在我的項目中。
您應該做的是創建一個BaseActivity
並使您的活動擴展此BaseActivity
。 添加此活動中的所有默認方法,以便您可以在任何地方使用它們。 您可以參考這個 Github項目以供參考。 它使用MVP。
這是BaseActivity的直接鏈接。
您只需要創建一個Common Utilities類。 只需將類復制並粘貼到您正在使用它的任何項目中。 只需將其方法訪問說明符設置為公共staic,以便您可以輕松訪問它。 例如
CommonUtilities.showToastMessage(String text);
我要做的是創建一個配置類並將所有這些小東西存儲在其中。 比如看看這個:
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的單個類。因此,您可以獲取進度對話框類的所有功能。
同樣地,對於吐司,你也可以這樣做。 如果您希望在創建活動時顯示它們,請保留以下內容:
MyProgressDialog dialog=new MyProgressDialog(this,"title","message");
dialog.show();
在你的活動的onCreate()方法中。 你也可以做同樣的吐司。
如果它是一個java類,只需創建一個構造函數並將該代碼段保存在該構造函數中。
您需要閱讀“文件模板” https://riggaroo.co.za/custom-file-templates-android-studio/這是一個很大的主題,但這是值得的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.