Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to take an EditText (numbers) then convert it to an integer to use it for math?

I am wondering how to take an EditText area that the user can enter a number in, then make it an integer so it can be used in the program for adding, subtracting, dividing, etc. Basicaly I need the test the enter to be able to be used in a calculator which will be within the code and then it needs to be put into a TextView or string so the final answer can be seen by the user.

More info: I have three EditText areas and the user has to fill in all three then press "equals" and it will tell them the answer, all the math and such needs to be in the code so the user just enters information and doesn't actually calculate anything. Thanks for the help, please ask me if you need any more info.

The best way to do this and the code would help me a lot.

I have added the code that was suggested but I still get an error on parseInt and I still need to add a button to calculate the numbers entered in the EditText areas. How would I do this? Sorry I am fairly to Java and Android development.

             package com.example.one;


             import com.example.one.R;

             import android.app.Activity;
             import android.content.Intent;
             import android.os.Bundle;
             import android.text.Editable;
             import android.view.View;
             import android.widget.Button;
             import android.widget.EditText;

             public class monthlyp extends Activity{

         @Override
        protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.monthlyp);


   EditText editone = (EditText) findViewById(R.id.editone);
   EditText editoneValue = (EditText) editone.getText();
   int editoneNum = Integer.parseInt(editoneValue);

    }
   ;

   }
like image 634
androidguy Avatar asked Aug 04 '11 21:08

androidguy


3 Answers

First, get an instance of the EditText:

EditText myEdit = (EditText) findViewById(R.id.edittext1);

Then get the string that is currently being displayed:

String myEditValue = myEdit.getText().toString();

Then parse it for an integer:

int myEditNum = Integer.parseInt(myEditValue);
like image 99
Krylez Avatar answered Nov 10 '22 15:11

Krylez


You have to change, in the property of your EditText, the Input type. For numbers, you have three choice: number, numberSigned or numberDecimal. It depends on what you want.

Then if you want to convert your string on your EditText to an integer, you can do:

int value = new Integer(myEditText.getText().toString()).intValue;
like image 29
Cedekasme Avatar answered Nov 10 '22 14:11

Cedekasme


EditText myEdit = (EditText) findViewById(R.id.edittext1);

String myEditValue = myEdit.getText().toString();

int myEditNum = Integer.parseInt(myEditValue);

myEditNum.setText(textOut1);

I am using the above to get user input as a number format. However when I try to display the int myEditNum in a TextView(textOut1) using the setText() method I get an error for the setText method (saying: ''Cannot invoke setText(TextView) on the primitive type int'').

I also tried (below) to convert int myEditNum to a String and then display it to a TextView but still doesn't work as I get the folloing error for the setText method: ''The method setText(TextView) is undefined for the type String''. EditText myEdit = (EditText) findViewById(R.id.edittext1);

String myEditValue = myEdit.getText().toString();

int myEditNum = Integer.parseInt(myEditValue);


String texting = Integer.toString(myEditNum);

texting.setText(textOut1);

Why does this happen and how do I solve it?

like image 23
kyllen87 Avatar answered Nov 10 '22 14:11

kyllen87