将值设置为空的编辑文本

时间:2013-04-16 11:10:16

标签: android

我有六对编辑文本,并非所有编辑文本都必须编辑,但至少必须编辑一对。 我想要做的是将空的那些设置为零,这样数学运算就可以了,但是编辑文本中没有显示0,所以用户不必继续删除零

目前我得到的并不理想

if (width1.getText().toString().equals("")
            || height1.getText().toString().equals("")
            || width2.getText().toString().equals("")
            || height2.getText().toString().equals("")
            || width3.getText().toString().equals("")
            || height3.getText().toString().equals("")
            || width4.getText().toString().equals("")
            || height4.getText().toString().equals("")
            || width5.getText().toString().equals("")
            || height5.getText().toString().equals("")
            || width6.getText().toString().equals("")
            || height6.getText().toString().equals("")) {
        width1.setText("0");
        width2.setText("0");
        width3.setText("0");
        width4.setText("0");
        width5.setText("0");
        width6.setText("0");
        height1.setText("0");
        height2.setText("0");
        height3.setText("0");
        height4.setText("0");
        height5.setText("0");
        height6.setText("0");
    }

    calc.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if (width1.getText().toString().equals("0")
                    || height1.getText().toString().equals("0")) {
                Toast msg = Toast.makeText(getBaseContext(),
                        "Please enter measurements", Toast.LENGTH_LONG);
                msg.show();
            }

            else {

                Intent sender = getIntent();
                int result100 = sender.getExtras().getInt("Result100");
                int result215 = sender.getExtras().getInt("Result215");
                int result325 = sender.getExtras().getInt("Result315");

                a = Double.parseDouble(width1.getText().toString());
                b = Double.parseDouble(height1.getText().toString());
                c = Double.parseDouble(width2.getText().toString());
                d = Double.parseDouble(height2.getText().toString());
                e = Double.parseDouble(width3.getText().toString());
                f = Double.parseDouble(height3.getText().toString());
                u = Double.parseDouble(width4.getText().toString());
                v = Double.parseDouble(height4.getText().toString());
                w = Double.parseDouble(width5.getText().toString());
                x = Double.parseDouble(height5.getText().toString());
                y = Double.parseDouble(width6.getText().toString());
                z = Double.parseDouble(height6.getText().toString());

                if (getIntent().hasExtra("Result100")) {
                    total = (result100) - (a * b) + (c * d) + (e * f)
                            + (u * v) + (w * x) + (y * z) * 60;
                } else if (getIntent().hasExtra("Result215")) {
                    total = (result215) - (a * b) + (c * d) + (e * f)
                            + (u * v) + (w * x) + (y * z) * 120;
                } else if (getIntent().hasExtra("Result315")) {
                    total = (result325) - (a * b) + (c * d) + (e * f)
                            + (u * v) + (w * x) + (y * z) * 180;

                }
                Intent intent = new Intent(MinusOpes.this,
                        Wl2Amounts.class);
                intent.putExtra("Result", total);
                startActivity(intent);
            }

1 个答案:

答案 0 :(得分:2)

您可以:

,而不是将每个字段设置为0
if (TextUtils.isEmpty(width1.getText().toString())) {
    a = 0;
} else {
    a = Double.parseDouble(width1.getText().toString());
}