[英]How do I separate my code into different files and call them within the onCreate method in android
我正在創建一個基本的Android應用程序來學習。 下面我有創建在創建幾個textViews的主要活動。 我試圖通過將大量代碼放入另一個名為“ CreateCategories.java”的文件中,然后調用函數,類或運行該代碼的內容來清理項目。 我該怎么做呢? 下面是我目前的程序
package com.company.practice;
import ...
.
.
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout categoryLayout = findViewById(R.id.categoryContainer);
ArrayList<Category> categories = new ArrayList<Category>();
categories.add(new Category("rent", "0.35", "0.00"));
categories.add(new Category("loan", "0.1", "0.00"));
TextView[] catogoryTitleTextView = new TextView[categories.size()];
TextView[] catogorypercentageTextView = new TextView[categories.size()];
TextView[] catogoryAmountTextView = new TextView[categories.size()];
for(int i =0; i < categories.size(); i++){
//initialize textviews
TextView title = new TextView(this);
TextView percentage = new TextView(this);
TextView amount = new TextView(this);
//set text views text, id, and textsize
title.setText(categories.get(i).title);
//title.setTextSize(getResources().getDimension(R.dimen.textsize));
title.setId(i + 100);
percentage.setText(categories.get(i).percent);
//percentage.setTextSize(getResources().getDimension(R.dimen.textsize));
percentage.setId(i + 200);
amount.setText(categories.get(i).amount);
//amount.setTextSize(getResources().getDimension(R.dimen.textsize));
amount.setId(i + 300);
//set params for title textview
RelativeLayout.LayoutParams titleParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
titleParams.addRule(RelativeLayout.ALIGN_END, R.id.salaryCategoryTextVeiw);
titleParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
titleParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
if(i==0){
//set params for title textview if it the first one. it sets below the textveiw catagory, and has more margin
titleParams.addRule(RelativeLayout.BELOW, R.id.salaryCategoryTextVeiw);
titleParams.topMargin = 27;
} else {
//this will look up the id of teh last category text view
titleParams.addRule(RelativeLayout.BELOW, catogoryTitleTextView[i-1].getId());
titleParams.topMargin = 15;
}
title.setLayoutParams(titleParams);
//set params for percentage textview
RelativeLayout.LayoutParams PercentParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
PercentParams.addRule(RelativeLayout.ALIGN_START, R.id.salaryPercentTextVeiw);
PercentParams.addRule(RelativeLayout.ALIGN_TOP, title.getId());
PercentParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
PercentParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
percentage.setLayoutParams(PercentParams);
//set params for amount textview
RelativeLayout.LayoutParams AmountParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
AmountParams.addRule(RelativeLayout.ALIGN_START, R.id.salaryAmountTextVeiw);
AmountParams.addRule(RelativeLayout.ALIGN_TOP, percentage.getId());
AmountParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
AmountParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
amount.setLayoutParams(AmountParams);
//add text views to layout
categoryLayout.addView(title);
categoryLayout.addView(percentage);
categoryLayout.addView(amount);
//save the views within the arrays
catogoryTitleTextView[i] = title;
catogorypercentageTextView[i] = percentage;
catogoryAmountTextView[i] = amount;
}
}
}
我希望它看起來像這樣:MainActivty.java
package com.company.practice;
import ...
.
.
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
CreateCategories.java
Function CreateCategories() {
RelativeLayout categoryLayout = findViewById(R.id.categoryContainer);
ArrayList<Category> categories = new ArrayList<Category>();
categories.add(new Category("rent", "0.35", "0.00"));
categories.add(new Category("loan", "0.1", "0.00"));
TextView[] catogoryTitleTextView = new TextView[categories.size()];
TextView[] catogorypercentageTextView = new TextView[categories.size()];
TextView[] catogoryAmountTextView = new TextView[categories.size()];
for(int i =0; i < categories.size(); i++){
//initialize textviews
TextView title = new TextView(this);
TextView percentage = new TextView(this);
TextView amount = new TextView(this);
//set text views text, id, and textsize
title.setText(categories.get(i).title);
//title.setTextSize(getResources().getDimension(R.dimen.textsize));
title.setId(i + 100);
percentage.setText(categories.get(i).percent);
//percentage.setTextSize(getResources().getDimension(R.dimen.textsize));
percentage.setId(i + 200);
amount.setText(categories.get(i).amount);
//amount.setTextSize(getResources().getDimension(R.dimen.textsize));
amount.setId(i + 300);
//set params for title textview
RelativeLayout.LayoutParams titleParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
titleParams.addRule(RelativeLayout.ALIGN_END, R.id.salaryCategoryTextVeiw);
titleParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
titleParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
if(i==0){
//set params for title textview if it the first one. it sets below the textveiw catagory, and has more margin
titleParams.addRule(RelativeLayout.BELOW, R.id.salaryCategoryTextVeiw);
titleParams.topMargin = 27;
} else {
//this will look up the id of teh last category text view
titleParams.addRule(RelativeLayout.BELOW, catogoryTitleTextView[i-1].getId());
titleParams.topMargin = 15;
}
title.setLayoutParams(titleParams);
//set params for percentage textview
RelativeLayout.LayoutParams PercentParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
PercentParams.addRule(RelativeLayout.ALIGN_START, R.id.salaryPercentTextVeiw);
PercentParams.addRule(RelativeLayout.ALIGN_TOP, title.getId());
PercentParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
PercentParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
percentage.setLayoutParams(PercentParams);
//set params for amount textview
RelativeLayout.LayoutParams AmountParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
AmountParams.addRule(RelativeLayout.ALIGN_START, R.id.salaryAmountTextVeiw);
AmountParams.addRule(RelativeLayout.ALIGN_TOP, percentage.getId());
AmountParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
AmountParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
amount.setLayoutParams(AmountParams);
//add text views to layout
categoryLayout.addView(title);
categoryLayout.addView(percentage);
categoryLayout.addView(amount);
//save the views within the arrays
catogoryTitleTextView[i] = title;
catogorypercentageTextView[i] = percentage;
catogoryAmountTextView[i] = amount;
}
任何有關如何執行此操作的有用建議將不勝感激。
您將必須實現一個將執行該工作的類,而您所要做的就是初始化您創建的類的對象並調用該類的特定功能。 如果您需要創建一些Toast或其他內容,則將上下文傳遞給該函數並在該函數中進行Toast。 但是不要忘記,您必須定義一個公共靜態上下文才能從外部類訪問它。
您正在做很多工作,以使代碼“更有條理”,但可讀性差得多。
要執行您想做的事情,您必須創建一個“庫”類(從不實例化,僅包含靜態方法),該類包含一個靜態函數,您可以從主onCreate調用它,並以某種方式返回實例化的所有內容,就像ArrayList,一個RelativeLayout和3個TextView數組,將它們分配給調用類中的變量,以便實例化它們后就可以使用它們。
如果要清理onCreate,請務必將代碼重構為另一個函數,放在同一類中,希望在原始代碼所在的位置附近,這樣,當您忘記值的位置時,不必在兩個文件之間查找在您的庫函數中創建的文件來自。
將代碼真正移入靜態庫函數的唯一時間是,如果您需要從多個類訪問該代碼多次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.