我在其末尾有5 Edittexts
和TextView
来计算EditTexts
中数据的平均值。我怎样才能除以只有输入的EditTexts
的数量?现在我除以总数EditTexts
,即5。
public class app extends ActionBarActivity {
EditText et1;
EditText et2;
EditText et3;
EditText et4;
EditText et5;
TextView sum;
double a;
double b;
double c;
double d;
double e;
double f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app);
et1 = (EditText) findViewById(R.id.et1);
et2 = (EditText) findViewById(R.id.et2);
et3 = (EditText) findViewById(R.id.et3);
et4 = (EditText) findViewById(R.id.et4);
et5 = (EditText) findViewById(R.id.et5);
sum = (TextView) findViewById(R.id.sum);
sum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (et1.getText().toString().isEmpty() ||
et2.getText().toString().isEmpty() ||
et3.getText().toString().isEmpty() ||
et4.getText().toString().isEmpty() ||
et5.getText().toString().isEmpty()) {
return;
}
else {
a = Double.parseDouble(et1.getText().toString());
b = Double.parseDouble(et2.getText().toString());
c = Double.parseDouble(et3.getText().toString());
d = Double.parseDouble(et4.getText().toString());
e = Double.parseDouble(et5.getText().toString());
f = (a+b+c+d+e) / 5;
sum.setText(Double.toString(f));}
}
});
}
答案 0 :(得分:1)
创建一个EditTexts列表,然后只获取非空的值并具有有效值。如下所示,使用按钮单击进行计算。
public class Main extends Activity {
EditText ed1, ed2, ed3, ed4, ed5;
TextView sum;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
ed1 = (EditText) findViewById(R.id.editText);
ed2 = (EditText) findViewById(R.id.editText2);
ed3 = (EditText) findViewById(R.id.editText3);
ed4 = (EditText) findViewById(R.id.editText4);
ed5 = (EditText) findViewById(R.id.editText5);
sum = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
final List<EditText> editTexts = new ArrayList<>();
editTexts.add(ed1);
editTexts.add(ed2);
editTexts.add(ed3);
editTexts.add(ed4);
editTexts.add(ed5);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int count = 0;
double total = 0;
double doubleSum = 0;
for (EditText editText : editTexts)
{
if (!editText.getText().toString().isEmpty())
{
if (isDouble(editText.getText().toString()))
{
total = total + Double.parseDouble(editText.getText().toString());
count++;
}
}
}
if (total != 0) doubleSum = total / count;
sum.setText(Double.toString(doubleSum));
}
});
}
public boolean isDouble(String string)
{
try
{
double mDouble = Double.parseDouble(string);
return true;
} catch (Exception e) {
return false;
}
}
}
答案 1 :(得分:0)
给出一个镜头,看看它是如何工作的......这将从父布局中获取所有EditText
并确定它是否包含某些内容。然后从那里做我们的计算。没有经过快速测试。
int count;
double totals;
double finaltotal;
View parent = findViewById(R.id.parent_layout);
for(int i = 0; i < parent.getChildCount(); i++) {
View singleChild = parent.getChildAt(i);
if(singleChild instanceof EditText) {
EditText mEditText = (EditText)singleChild;
if(mEditText.getText().length() > 0){
count = count + 1;
totals = totals + Double.parseDouble(mEditText.getText().toString());
}
}
}
finaltotal = totals / count;
sum.setText(Double.toString(finaltotal));
答案 2 :(得分:0)
如果你只有5个并且你只计划中只有5个你可以轻松做到这样的事情:
et1 = (EditText) findViewById(R.id.et1);
et2 = (EditText) findViewById(R.id.et2);
et3 = (EditText) findViewById(R.id.et3);
et4 = (EditText) findViewById(R.id.et4);
et5 = (EditText) findViewById(R.id.et5);
int b1 = et1.getText().toString().isEmpty() ? 1 : 0;
int b2 = et2.getText().toString().isEmpty() ? 1 : 0;
int b3 = et3.getText().toString().isEmpty() ? 1 : 0;
int b4 = et4.getText().toString().isEmpty() ? 1 : 0;
int b5 = et5.getText().toString().isEmpty() ? 1 : 0;
int sum = b1 + b2 + b3 + b4 + b5 ;
但是,如果你现在只有5个,建议将它们存储在数组中,然后进入(几乎相同的代码)for循环
int sum = 0;
for (EditText e : list) {
sum = sum + e.getText().toString().isEmpty() ? 1 : 0;
}