0

例如我有..

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final TextView excellent_val = (TextView)findViewById(R.id.excellent_val);
    TextView best_val = (TextView)findViewById(R.id.best_val);
    TextView better_val = (TextView)findViewById(R.id.better_val);
    TextView good_val = (TextView)findViewById(R.id.good_val);
    TextView poor_val = (TextView)findViewById(R.id.poor_val);

    final EditText  respondents = (EditText)findViewById(R.id.respondents);
    final EditText  questions = (EditText)findViewById(R.id.questions);
    Button button1 = (Button)findViewById(R.id.btn);




    button1.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub








            String c = questions.getText().toString();
            final Integer count = Integer.parseInt(c);

            questions.setText(Integer.toString(count));
            respondents.setText(respondents.getText().toString());

            final TableLayout table = new TableLayout(getApplicationContext());
            table.setVerticalScrollBarEnabled(true);
            table.setPadding(10, 10, 10, 10);




            final TableRow tableRow = new TableRow (getApplicationContext());               


            TextView txt = new TextView (getApplicationContext());
            TextView txt2 = new TextView (getApplicationContext());
            TextView txt3 = new TextView (getApplicationContext());
            TextView txt4 = new TextView (getApplicationContext());
            TextView txt5 = new TextView (getApplicationContext());
            TextView txt6 = new TextView (getApplicationContext());

            tableRow.addView(txt);
            tableRow.addView(txt2);
            tableRow.addView(txt3);
            tableRow.addView(txt4);
            tableRow.addView(txt5);
            tableRow.addView(txt6);



            tableRow.setBackgroundColor(Color.GRAY);

            txt.setText("Question  ");
            txt2.setText("Excellent   ");
            txt3.setText("Best     ");
            txt4.setText("Better   ");
            txt5.setText("Good     ");
            txt6.setText("Poor     ");

            txt.setTextColor(Color.BLACK);
            txt2.setTextColor(Color.BLACK);
            txt3.setTextColor(Color.BLACK);
            txt4.setTextColor(Color.BLACK);
            txt5.setTextColor(Color.BLACK);
            txt6.setTextColor(Color.BLACK);


            table.addView(tableRow);





            final StringBuilder output = new StringBuilder();

            int j=0;
            for(j = 1; j<=count; j++){

                Random rnd = new Random(); 
                int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); 

                 tableRow2  = new TableRow (getApplicationContext());
                 excellent = new EditText (getApplicationContext());
                 best = new EditText (getApplicationContext());
                 better = new EditText (getApplicationContext());
                 good = new EditText (getApplicationContext());
                 poor = new EditText (getApplicationContext());
                 mean_in = new TextView(getApplicationContext());

                 name = new TextView (getApplicationContext());

                excellent.setBackgroundColor(color);
                best.setBackgroundColor(color);
                better.setBackgroundColor(color);
                good.setBackgroundColor(color);
                poor.setBackgroundColor(color);


                name.setText("Q#"+Integer.toString(j));
                mean_in.setTextColor(Color.WHITE);

                tableRow2.addView(name);
                tableRow2.addView(excellent);
                tableRow2.addView(best);
                tableRow2.addView(better);
                tableRow2.addView(good);
                tableRow2.addView(poor);
                tableRow2.addView(mean_in);
                table.addView(tableRow2);


                excellentList.add(excellent);
                bestList.add(best);
                betterList.add(better);
                goodList.add(good);
                poorList.add(poor);

                mean_array.add(excellent);
                mean_array.add(best);
                mean_array.add(better);
                mean_array.add(good);
                mean_array.add(poor);

                MEAN.add(mean_in);



            }


            //Make an ArrayList of EditText
            //Put all excellent EditTexts in it.
            //In the onClick go through this list and append all the getText().toString() of these EditTexts




            tableRow1 = new TableRow (getApplicationContext());

            final Button get = new Button(getApplicationContext());
            tableRow1.addView(get);
            get.setText("Get!");
            get.setTextSize(8);




             //******************************************************************************// 
            //                              GET!                                    //  
           //******************************************************************************//

            get.setOnClickListener(new OnClickListener(){




                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    String population = respondents.getText().toString();
                    double n = Double.parseDouble(population);

                    double Population = 0;
                    double final_Population =0;
                        Population = n/(1+(n*(0.003*0.003)));
                        final_Population = Math.ceil(Population);

                    String val_excellent=null;
                    double weigthed_ex=0;
                    double result =0;
                    double final_result=0;



                    for(EditText excellent : excellentList){
                        val_excellent= excellent.getText().toString(); 
                        double values = Double.parseDouble(val_excellent);
                        for(int z=0;z<val_excellent.length();z++){
                            weigthed_ex =values*5/final_Population;
                        }
                        String weight_excellent =String.format("%.3g",weigthed_ex);
                        get.setEnabled(false);
                        excellent.setTextSize(11);
                        excellent.setTextColor(Color.BLACK);
                        excellent.setEnabled(false);
                        excellent.setText((weight_excellent));
                      }


                    String val_best=null;
                    double weigthed_best=0;
                    for(EditText best: bestList){
                        val_best = best.getText().toString();
                        double values_best = Double.parseDouble(val_best);
                        for(int y =0; y<val_best.length();y++){
                            weigthed_best = values_best*4/final_Population;
                        }
                    String weight_best =String.format("%.3g",weigthed_best);    
                    best.setTextSize(11);
                    best.setTextColor(Color.BLACK);
                    best.setEnabled(false);
                    best.setText(weight_best);

                    }



                    String val_better=null;
                    double weigthed_better=0;
                    for(EditText better: betterList){
                        val_better = better.getText().toString();
                        double values_better = Double.parseDouble(val_best);
                        for(int k =0; k<val_better.length();k++){
                            weigthed_best = values_better*3/final_Population;
                        }
                    String weight_better =String.format("%.3g",weigthed_better);    
                    better.setTextSize(11);
                    better.setTextColor(Color.BLACK);
                    better.setEnabled(false);
                    better.setText(weight_better);

                    }


                    String val_good=null;
                    double weigthed_good=0;
                    for(EditText good: goodList){
                        val_good = good.getText().toString();
                        double values_good = Double.parseDouble(val_good);
                        for(int l =0; l<val_good.length();l++){
                            weigthed_good = values_good*2/final_Population;
                        }
                    String weight_good =String.format("%.3g",weigthed_good);    
                    good.setTextSize(11);
                    good.setTextColor(Color.BLACK);
                    good.setEnabled(false);
                    good.setText(weight_good);

                    }

                    String val_poor=null;
                    double weigthed_poor=0;
                    for(EditText poor: poorList){
                        val_poor = poor.getText().toString();
                        double values_poor = Double.parseDouble(val_poor);
                        for(int m =0; m<val_poor.length();m++){
                            weigthed_poor = values_poor*1/final_Population;
                        }
                    String weight_poor =String.format("%.3g",weigthed_poor);    
                    poor.setTextSize(11);
                    poor.setTextColor(Color.BLACK);
                    poor.setEnabled(false);
                    poor.setText(weight_poor);
                    }

                    Button getMean = new Button(getApplicationContext());
                    tableRow1.addView(getMean);

                    getMean.setOnClickListener(new OnClickListener(){

                        @Override
                        public void onClick(View arg0) {
                            // TODO Auto-generated method stub

                            String a=null;
                            String b=null;
                            String c=null;
                            String d=null;
                            String e=null;

                            double f=0;
                            double g=0;
                            double h=0;
                            double i=0;
                            double j=0;

                            double mean=0;

                            for(TextView mean_in: MEAN){
                                for(EditText excellent :excellentList){
                                    a = excellent.getText().toString();
                                    f= Double.parseDouble(a);
                                }

                                for(EditText best :bestList){
                                    b = best.getText().toString();
                                    g= Double.parseDouble(b);
                                }
                                for(EditText better :betterList){
                                    c = better.getText().toString();
                                    h= Double.parseDouble(c);
                                }
                                for(EditText good :goodList){
                                    d = good.getText().toString();
                                    i= Double.parseDouble(d);
                                }
                                for(EditText poor :poorList){
                                    e = poor.getText().toString();
                                    j= Double.parseDouble(e);
                                }







                                mean = f+g+h+i+j/5;
                                mean_in.setText(" ");
                              }

                        }

                    });



                            TextView mean = new TextView(getApplicationContext());
                            mean.setText("Mean");
                            mean.setTextColor(Color.RED);
                            tableRow.addView(mean);

                    }  


    });

我希望我的 5 个编辑文本的总值显示在 mean_in(TextView) 中我该怎么做?请帮帮我...我很难想我该怎么做...非常感谢。 ..



在此处输入图像描述

4

3 回答 3

1
   Look at the example i provide here 

    String text="";
    for(j = 1; j<=arraylist.size; j++)
    {
    text=text+arraylist.get(j);
    }
    mean_in.setText(text);
于 2013-11-14T09:10:00.887 回答
1

我认为您正在寻找类似Integer.parseInt(String s) This 将允许您从所有 editText 元素中获取 int 的内容,然后您只需将它们全部添加在一起。

于 2013-11-14T09:57:10.180 回答
0

此代码将显示平均值字段中的平均值:

//Get text from each field
String sExcellent = this.excellent_val.getText().toString();
String sBest = this.best_val.getText().toString();
String sBetter = this.better_val.getText().toString();
String sGood = this.good_val.getText().toString();
String sPoor = this.poor_val.getText().toString();

//convert to float while adding to array (although it would be better do some checking first)

float frequencies = new float[]{Float.parseFloat(sExcellent),
                                Float.parseFloat(sBest),
                                Float.parseFloat(sBetter),
                                Float.parseFloat(sGood),
                                Float.parseFloat(sPoor)};

//calculate average
float totalFrequency = 0;
for(float frequency : frequencies)
    totalFrequency += frequency;

//set text of mean_in to mean frequency
mean_in.setText(""+totalFrequency/frequencies.length);

尖端

我必须建议您更好地组织代码!

  • 你的 onCreate() 太长了!您应该将大型任务分配给许多方法,例如创建用户界面()方法来设置用户界面。我建议阅读有关设计模式或代码重构的书。

  • 变量应该有有意义的名字。像“c”和“d”这样的变量名是懒惰的、令人困惑的并且被认为是非常糟糕的做法。

  • 我不明白您是如何创建布局的。你让自己很难受。这是一个非常简短的简单视频教程,解释了如何在 XML 中创建 TableLayout 并在 Java 中对其进行初始化。

希望这有帮助,祝你好运!

于 2013-11-15T07:59:16.787 回答