如何检查Android EditText整数值?

时间:2013-05-20 22:02:30

标签: java android

我正在尝试制作一个可以计算给定人的所得税的应用。有一个EditText字段,用户必须指出他的月工资,然后在MainActivity.java中计算。问题是 - 当我尝试使用给定的输入字段进行一些数学运算时,会出现错误The operator * is undefined for the argument type(s) EditText, double

wage = (EditText) findViewById(R.id.wage);
submit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            wageAfterTax = wage * 0.25;
            result.setText("Your wage after tax deductions is " + wageAfterTax);
        }
    });

你能建议做什么吗?

1 个答案:

答案 0 :(得分:4)

wage是对EditText对象的引用。您需要从中获取文本并将其转换为数字。

wage = (EditText) findViewById(R.id.wage);
submit.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String wageText = wage.getText().toString();
        double wageAmount = Double.valueOf(wageText);
        wageAfterTax = wageAmount * 0.25;
        result.setText("Your wage after tax deductions is " + wageAfterTax);
    }
});