简体   繁体   中英

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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