1

好吧,我的应用程序中的计算是这样的..

有一个编辑文本框,其中来自用户的 input(number) 被解析为 int 作为nos

根据输入,出现另外两组编辑文本框。

如果 nos 为 3,则出现来自 arrayOfEditText 的 3 个 edittext 和来自 arrayOfEditText1 的 3 个 edittext.. 并且在这些 edittext 集中输入的值用于计算。

不完整的java代码如下..

    calculate.setOnClickListener(new View.OnClickListener() {
                            @Override public void onClick(View v) {
                    // TODO Auto-generated method stub
                    chk();          
}       
});     
}   
public void chk()   {   
            EditText[] arrayOfEditText = new EditText[11];
            arrayOfEditText[1] = ((EditText)findViewById(R.id.EditText01));
            arrayOfEditText[2] = ((EditText)findViewById(R.id.EditText02));
            arrayOfEditText[3] = ((EditText)findViewById(R.id.EditText03));
            arrayOfEditText[4] = ((EditText)findViewById(R.id.EditText04));
            arrayOfEditText[5] = ((EditText)findViewById(R.id.EditText05));
            arrayOfEditText[6] = ((EditText)findViewById(R.id.EditText06));
            arrayOfEditText[7] = ((EditText)findViewById(R.id.EditText07));
            arrayOfEditText[8] = ((EditText)findViewById(R.id.EditText08));
            arrayOfEditText[9] = ((EditText)findViewById(R.id.EditText09));
            arrayOfEditText[10] = ((EditText)findViewById(R.id.EditText10));

            EditText[] arrayOfEditText1 = new EditText[11];
            arrayOfEditText1[1] = ((EditText)findViewById(R.id.EditText11));
            arrayOfEditText1[2] = ((EditText)findViewById(R.id.EditText12));
            arrayOfEditText1[3] = ((EditText)findViewById(R.id.EditText13));
            arrayOfEditText1[4] = ((EditText)findViewById(R.id.EditText14));
            arrayOfEditText1[5] = ((EditText)findViewById(R.id.EditText15));
            arrayOfEditText1[6] = ((EditText)findViewById(R.id.EditText16));
            arrayOfEditText1[7] = ((EditText)findViewById(R.id.EditText17));
            arrayOfEditText1[8] = ((EditText)findViewById(R.id.EditText18));
            arrayOfEditText1[9] = ((EditText)findViewById(R.id.EditText19));
            arrayOfEditText1[10] = ((EditText)findViewById(R.id.EditText20));

            for(int i=1;i<=nos;i++)
            {
                if(arrayOfEditText[i].getText().toString().equals("")||arrayOfEditText1[i].getText().toString().equals(""))
                {
                    Toast.makeText(getApplicationContext(), "Dont leave points empty", 0).show();
                }
                else
                {
                    calcul();
                }
            }   }   public void calcul()    {       EditText[] arrayOfEditText = new EditText[11];
            arrayOfEditText[1] = ((EditText)findViewById(R.id.EditText01));
            arrayOfEditText[2] = ((EditText)findViewById(R.id.EditText02));
            arrayOfEditText[3] = ((EditText)findViewById(R.id.EditText03));
            arrayOfEditText[4] = ((EditText)findViewById(R.id.EditText04));
            arrayOfEditText[5] = ((EditText)findViewById(R.id.EditText05));
            arrayOfEditText[6] = ((EditText)findViewById(R.id.EditText06));
            arrayOfEditText[7] = ((EditText)findViewById(R.id.EditText07));
            arrayOfEditText[8] = ((EditText)findViewById(R.id.EditText08));
            arrayOfEditText[9] = ((EditText)findViewById(R.id.EditText09));
            arrayOfEditText[10] = ((EditText)findViewById(R.id.EditText10));

            EditText[] arrayOfEditText1 = new EditText[11];
            arrayOfEditText1[1] = ((EditText)findViewById(R.id.EditText11));
            arrayOfEditText1[2] = ((EditText)findViewById(R.id.EditText12));
            arrayOfEditText1[3] = ((EditText)findViewById(R.id.EditText13));
            arrayOfEditText1[4] = ((EditText)findViewById(R.id.EditText14));
            arrayOfEditText1[5] = ((EditText)findViewById(R.id.EditText15));
            arrayOfEditText1[6] = ((EditText)findViewById(R.id.EditText16));
            arrayOfEditText1[7] = ((EditText)findViewById(R.id.EditText17));
            arrayOfEditText1[8] = ((EditText)findViewById(R.id.EditText18));
            arrayOfEditText1[9] = ((EditText)findViewById(R.id.EditText19));
            arrayOfEditText1[10] = ((EditText)findViewById(R.id.EditText20));
                for(i=1;i<=nos;i++)
                {
                  //perform calculation
                }   }

现在要在//perform 计算区域执行的逻辑是..

arrayofedittext[1]*arrayofedittext1[1] + arrayofedittext[2]*arrayofedittext1[2] + arrayofedittext[3]*arrayofedittext1[3] + etc 基于nos / arrayofedittext[1] + arrayofedittext[2] + arrayofedittext[3] +等基于nos

那么有人可以帮我完成这个编码吗?:)

4

2 回答 2

1

创建全局变量

int[] intEditText new int[nos+1];
int[] intEditText1 new int[nos+1];
....
public void chk()   {   
int test=0;
try {

 for(int i=1;i<=nos+nos;i++) {
  if (i==1 && ((EditText)findViewById(R.id.EditText01))=="") {test=1;}
  if (i==2 && ((EditText)findViewById(R.id.EditText02))=="") {test=1;}
   .....etc.
  if (i==11 && ((EditText)findViewById(R.id.EditText11))=="") {test=1;}
  if (test==1) {
    Toast.makeText(getApplicationContext(), "Dont leave points empty", 0).show();
    break;
  } else {
    if (i==1) {intEditText[i]:=Integer.parseInt(((EditText)findViewById(R.id.EditText1));}
    if (i==2) {intEditText[i]:=Integer.parseInt(((EditText)findViewById(R.id.EditText2));}
    .... etc.
    if (i==11) {intEditText1[i-nos]:=Integer.parseInt(((EditText)findViewById(R.id.EditText11));}
    ....etc.

     calcul();
  {
 }


 } catch(NumberFormatException nbre) {
   System.out.println("Could not parse " + nbre);
 } 
}

public void calcul()    {
int myCalc;

  myCalc = intEditText[1]*intEditText[2]..... / (intEditText[1]*intEditText[2].....)
   //perform calculation
}
于 2012-06-16T17:03:37.090 回答
1

您执行相同操作的次数越少,您的应用程序运行速度就越快。所以 makeEditText[] arrayOfEditTextEditText[] arrayOfEditText1类范围的变量,在 onCreate() 中定义一次,之后你就不必使用 findViewById() 了R.id.EditText**

这是一个检查计算中的任何行是否为空的函数,如果不是,那么您total将拥有您想要的。

public void calculateTotal() {
    String first;
    String second;

    double firstNum;
    double numerator = 0;
    double denominator = 0;

    // This loops counts from nos down to 1. It checks for empty strings and 
    //   sums up the numerator and denominator along the way.
    for(int index = nos; index > 0; index--) {
        first = arrayOfEditText[index].getText().toString();
        second = arrayOfEditText1[index].getText().toString();

        if(first.isEmpty() || second.isEmpty()) {
            Toast.makeText(getApplicationContext(), "Dont leave any points empty (" + (index + 1) + ")", Toast.LENGTH_LONG).show();
            return; // Invalid input, warn user and bail out of function!
        }

        firstNum = Double.parseDouble(first);
        numerator += firstNum * Double.parseDouble(second);
        denominator += firstNum;
    }

    // Do something with total
    double total = numerator / denominator;
}

更精细的点

定义arrayOfEditTextarrayOfEditText1在整个类中可见,如下所示:

public class Example extends Activity {
    EditText[] arrayOfEditText = new EditText[11];
    EditText[] arrayOfEditText1 = new EditText[11];
    ...

        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            ...  
            arrayOfEditText[1] = (EditText) findViewById(R.id.EditText01);
            etc, etc...

你应该知道数组是一个零索引列表。arrayOfEditText因此,您的数组 (和arrayOfEditText1) 中的第一个条目是null,如果您尝试引用arrayOfEditText[0]期望 EditText ,您的应用程序将会崩溃。

最后,此代码假定每个 EditText 都是有效的 Double,如果您不限制非数字条目,则添加检查非常简单。

于 2012-06-16T17:14:42.673 回答