如何计算EditText框的总和?

时间:2014-02-05 12:06:19

标签: android textview calculator

大家好我处理这个问题时遇到了一些麻烦。我有5个EditText框,我要求用户在其中加上一个数字。然后我想计算EditText框的总和,并用总和做一些事情。但是,如果用户没有在所有方框中输入数字,我遇到了麻烦。

是否可以在框中设置默认值“0”以防万一不是所有的框都被填充但是要计算的总和虽然不是所有的框都被填充了?

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_extra_question);       

    textBox1 = (EditText) findViewById(R.id.editText1);
    textBox2 = (EditText) findViewById(R.id.editText2);
    textBox3 = (EditText) findViewById(R.id.editText3);
    textBox4 = (EditText) findViewById(R.id.editText4);
    textBox5 = (EditText) findViewById(R.id.editText5);
    buttON = (Button) findViewById(R.id.button1);

    buttON.setOnClickListener(new OnClickListener()
    {


        @Override
        public void onClick(View v)
        {               
            number1 = Integer.parseInt(textBox1.getText().toString());
            number2 = Integer.parseInt(textBox2.getText().toString());
            number3 = Integer.parseInt(textBox3.getText().toString());
            number4 = Integer.parseInt(textBox4.getText().toString());
            number5 = Integer.parseInt(textBox5.getText().toString());

            sum = number1 + number2 + number3 + number4 + number5;
            if(sum > 100)
            {                   
                Toast.makeText(getApplicationContext(), "Nice!", Toast.LENGTH_LONG).show();
            }
            else
            {
                Toast.makeText(getApplicationContext(), "Fail!",   Toast.LENGTH_LONG).show();
            }
        }

    });

4 个答案:

答案 0 :(得分:2)

为每个号码执行此操作:

number1 = textBox1.getText().toString().trim().isEmpty()?0:Integer.parseInt(textBox1.getText().trim().toString());

答案 1 :(得分:2)

按照这样的功能创建

public int getIntFromString(String str) {

    if (str != null) {

        if (str.equalsIgnoreCase("")) {
            return 0;
        } else {

            try {
                return Integer.parseInt(str);
            } catch (NumberFormatException e) {
                return 0;
            }
        }
    } else {
        return 0;
    }
}

那么你的代码应该是

        number1 = getIntFromString(textBox1.getText().toString().trim());
        number2 = getIntFromString(textBox2.getText().toString().trim());
        number3 = getIntFromString(textBox3.getText().toString().trim());
        number4 = getIntFromString(textBox4.getText().toString().trim());
        number5 = getIntFromString(textBox5.getText().toString().trim());

答案 2 :(得分:0)

是的,你的想法是正确的。对于每个编辑文本,只需检查:

if(textBox1.getText().toString().equals(""))
{
      number1 = 0;
}
else
    number1 = Integer.parseInt(textBox1.getText().toString());

答案 3 :(得分:0)

您必须使用以下代码检查空字符串,并在每个缺失值中添加“0”。

number1 = textBox1.getText().toString().isEmpty()?0:Integer.parseInt(textBox1.getText().toString());
number2 = textBox2.getText().toString().isEmpty()?0:Integer.parseInt(textBox2.getText().toString());
number3 = textBox3.getText().toString().isEmpty()?0:Integer.parseInt(textBox3.getText().toString());
number4 = textBox4.getText().toString().isEmpty()?0:Integer.parseInt(textBox4.getText().toString());
number5 = textBox5.getText().toString().isEmpty()?0:Integer.parseInt(textBox5.getText().toString());