使用Java在Android中添加20个文本视图

使用Java在Android中添加20个文本视图,java,android,loops,for-loop,Java,Android,Loops,For Loop,我是Android编程新手,目前正试图理解在Android中实现简单Java。 我想做的是了解如何在Android studio中添加20个文本视图。我是说,这里最好的选择是什么。 是: a) 手动添加20个文本视图,当我点击按钮a时,所有文本视图都将更新为公式。我相信这会产生不必要的java代码和重复 b) 创建for循环,添加一个textView,然后通过方法自动更新。我到目前为止所做的一切。下面的代码(IMEngine)有一个按钮连接到MainCalculate ID: double re

我是Android编程新手,目前正试图理解在Android中实现简单Java。 我想做的是了解如何在Android studio中添加20个文本视图。我是说,这里最好的选择是什么。 是:

a) 手动添加20个文本视图,当我点击按钮a时,所有文本视图都将更新为公式。我相信这会产生不必要的java代码和重复

b) 创建for循环,添加一个textView,然后通过方法自动更新。我到目前为止所做的一切。下面的代码(IMEngine)有一个按钮连接到MainCalculate ID:

double realPrice = 10;
double total = (realPrice * 2)+1;
double increase = 0.1;
TextView percentageTextView = (TextView) findViewById(R.id.percentageTextViewID);


public void MainCalculate (View view) {

    MarketValueAnswer.setText(Double.toString(marketValueAnswer));

    for (double i=realPrice; i<=total; i+=increase) {

        double formula = ((realPrice*increase) + realPrice);
        increase+=0.05;
        percentageTextView.append("\n" + formula);

    }
double realPrice=10;
双倍总额=(真实价格*2)+1;
双倍增长=0.1;
TextView percentageTextView=(TextView)findViewById(R.id.percentageTextViewID);
公共void main计算(视图){
setText(Double.toString(MarketValueAnswer));

对于(double i=realPrice;i如果你真的想添加20个文本视图,你可以这样做。 单击butten时调用函数calculation()

double realPrice = 10;
double total = (realPrice * 2)+1;
double increase = 0.1;

private void calculation() {

    for (double i=realPrice; i<=total; i+=increase) {

        double formula = ((realPrice*increase) + realPrice);
        increase+=0.05;
        try {
            TextView txtView = new TextView(this);
            txtView.setText(Double.toString(formula));
            ll.addView(txtView);
        } catch (Exception ignored) {}
    }
}
double realPrice=10;
双倍总额=(真实价格*2)+1;
双倍增长=0.1;
私人空隙计算(){

对于(double i=realPrice;i如果你真的想添加20个文本视图,你可以这样做。 单击butten时调用函数calculation()

double realPrice = 10;
double total = (realPrice * 2)+1;
double increase = 0.1;

private void calculation() {

    for (double i=realPrice; i<=total; i+=increase) {

        double formula = ((realPrice*increase) + realPrice);
        increase+=0.05;
        try {
            TextView txtView = new TextView(this);
            txtView.setText(Double.toString(formula));
            ll.addView(txtView);
        } catch (Exception ignored) {}
    }
}
double realPrice=10;
双倍总额=(真实价格*2)+1;
双倍增长=0.1;
私人空隙计算(){

对于(double i=realPrice;i,这里有一个更好的使用AsynchTask的方法。这将防止您的应用程序冻结

calculation myCalc = new calculation(this);
myCalc.execute();
下面是AsyncTask类

private class calculation extends AsyncTask<Void, Double, Void> {
        //we need this context for the TextView instantiation

    private Context mContext;

    private calculation(Context context){
        mContext = context;
    }

    @Override
    protected Void doInBackground(Void... voids) {
        for (double i=realPrice; i<=total; i+=increase) {
            double formula = ((realPrice*increase) + realPrice);
            increase+=0.05;
            publishProgress(formula);
        }
        return null;
    }

    protected void onProgressUpdate(Double... s) {
        try {
            TextView txtView = new TextView(mContext);
            txtView.setText(Double.toString(s[0]));
            ll.addView(txtView);
        } catch (Exception ignored) {}
    }
}
私有类计算扩展了异步任务{
//我们需要这个上下文来进行TextView实例化
私有上下文;
私有计算(上下文){
mContext=上下文;
}
@凌驾
受保护的空位背景(空位…空位){

对于(double i=realPrice;i,这里有一个更好的使用AsynchTask的方法。这将防止您的应用程序冻结

calculation myCalc = new calculation(this);
myCalc.execute();
下面是AsyncTask类

private class calculation extends AsyncTask<Void, Double, Void> {
        //we need this context for the TextView instantiation

    private Context mContext;

    private calculation(Context context){
        mContext = context;
    }

    @Override
    protected Void doInBackground(Void... voids) {
        for (double i=realPrice; i<=total; i+=increase) {
            double formula = ((realPrice*increase) + realPrice);
            increase+=0.05;
            publishProgress(formula);
        }
        return null;
    }

    protected void onProgressUpdate(Double... s) {
        try {
            TextView txtView = new TextView(mContext);
            txtView.setText(Double.toString(s[0]));
            ll.addView(txtView);
        } catch (Exception ignored) {}
    }
}
私有类计算扩展了异步任务{
//我们需要这个上下文来进行TextView实例化
私有上下文;
私有计算(上下文){
mContext=上下文;
}
@凌驾
受保护的空位背景(空位…空位){

for(double i=realPrice;i通过循环创建文本视图。这是一种更好的方法。

通过循环创建文本视图。这是一种更好的方法。

好的,你问了我的问题。我想知道我是应该使用for循环还是手动添加20个文本视图。所以答案是“使用循环”然后将它们格式化为xml/java。谢谢。是的,是的。请参阅下面我的示例postet以获得最佳解决方案。好的,你问了我的问题。我想知道我是应该使用for循环还是手动添加20个TextView。因此答案是“使用循环”,然后将它们格式化为xml/java。谢谢,是的。请参阅下面我的示例postet以获得最佳解决方案。