如何使用数组或arraylist计算edittextviews [英] How do I use an array or an arraylist to calculate edittextviews
问题描述
大家好,
我是Android和Java编码的新手。我一直试图想出如何使用
取4个EditText框并使用数组或ArrayList计算它们
然后将计算显示到另一个EditText框。
我使用double来存储Numbers,因为它们将具有小数。
这是布局 - 2017-10-17_1457 [ ^ ]
我尝试过:
我使用
双等级1;
双等级2;
双等级3;
双等级4;
double totalGrade;
将其存储在全球区域
我google了甚至检查了youtube我找不到任何帮助我的东西。我的意思是有一些例子,但我没理解。
Hi Everyone,
Im brand new to Android and Java coding. Ive been trying to figure how to
Take 4 EditText boxes and calculate them using an array or ArrayList
Then display the Calculation to another EditText Box.
I used double to store the Numbers as they will have a decimal.
Here is the layout - 2017-10-17_1457[^]
What I have tried:
I use
double grade1;
double grade2;
double grade3;
double grade4;
double totalGrade;
To store it in the Global area
I googled and even checked youtube and i couldnt find anything that helped me. I mean there are some examples but nothing i understood.
推荐答案
所以你可以将你所有的等级
变量放到aarrayList
并迭代数组得到总和。
So you can put all yourgrade
variables to aarrayList
and iterate the array to get the sum.
double totalGrade=0;
ArrayList<Double> arr =new ArrayList();//create a arraylist
//adding values to the arraylist
arr.add(grade1);
arr.add(grade2);
arr.add(grade3);
arr.add(grade4);
//Iterating through the array list to get sum
for(Double d:arr){
totalGrade+=d;
}
System.out.println("Total : "+double totalGrade);
你要做的是如此,如此微不足道,我不知道你怎么能在任何时间搜索所有,并没有找到数十个,如果不是数百个例子。无论如何...
在计算成绩按钮的onClick()
处理程序中,调用getText()
每个EditText
小部件的方法获取其值,然后调用Double.parseDouble ()
转换该值并将其分配给每个等级#
变量。例如:
What you are trying to do is so, so trivial, I don't know how you could've searched any length of time at all and not have found dozens, if not hundreds, of examples. Anyway...
In theonClick()
handler for the "Calculate Grade" button, call thegetText()
method for eachEditText
widget to get its value, and then callDouble.parseDouble()
to convert that value and assign it to each of yourgrade#
variables. For example:
EditText et = (EditText) findViewById(R.id.grade1);
grade1 = Double.parseDouble(et.getText().toString());
由于它是只读的,你应该考虑改变百分比 EditText
而不是 TextView
小部件。然后你可以像这样分配总和:
Since it is read only, you should consider change the "Percentage" EditText
to be a TextView
widget instead. Then you can assign the sum to it like:
TextView tv = (TextView) findViewById(R.id.pct);
tv.setText(String.valueOf(grade1 + ...));
这篇关于如何使用数组或arraylist计算edittextviews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!