简体   繁体   English

如何检查Android EditText整数值?

[英]How to check Android EditText integer value?

I'm trying to make an app that would calculate the income tax of a given person. 我正在尝试制作一个可以计算给定人的所得税的应用程序。 There is an EditText field where the user must point out his monthly wage and then it's being calculated in the MainActivity.java. 有一个EditText字段,用户必须指出他的每月工资,然后在MainActivity.java中进行计算。 The problem is - when I try to do some maths using the given input field, an error The operator * is undefined for the argument type(s) EditText, double shows up. 问题是-当我尝试使用给定的输入字段进行一些数学The operator * is undefined for the argument type(s) EditText, double ,错误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);
        }
    });

Can you please suggest what to do? 你能建议做什么吗?

wage is a reference to an EditText object. wage是对EditText对象的引用。 You need to get the text from it and convert that to a number first. 您需要从中获取文本并将其首先转换为数字。

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);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM