0

我正在创建一个基本的 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);
    }
}

创建类别.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;
    }

任何有关如何做到这一点的有用建议将不胜感激。

4

2 回答 2

0

您正在做很多工作以使您的代码“更有条理”,但可读性要差得多。

要执行您想做的事情,您必须创建一个“库”类(从不实例化,仅包含静态方法),其中包含您将从主 onCreate 调用的静态函数,返回您以某种方式实例化的所有内容,这看起来像ArrayList、RelativeLayout 和 3 个 TextView 数组,将它们分配给调用类中的变量,以便在实例化它们后可以使用这些东西......

如果您想清理 onCreate,请务必将代码重构为同一个类中的另一个函数,希望靠近原始代码所在的位置,这样当您忘记值的位置时,您不必在两个文件之间查看在您的库函数中创建的那些来自。

您真正应该将代码移动到静态库函数中的唯一一次是您需要从多个类中多次访问该代码。

于 2018-03-16T21:50:52.070 回答
0

您必须实现一个将执行该工作的类,您所要做的就是初始化您创建的类的对象并调用该类的特定函数。如果您需要创建一些 Toast 或其他东西,您会将上下文传递给该函数并在该函数中制作 toast。但不要忘记,您必须定义一个公共静态上下文才能从外部类访问它。

于 2018-03-16T19:59:28.347 回答