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

I am wondering how to take the EditText area into which the user can enter a number, and then make it integer so that it can be used in the program to add, subtract, divide, etc. Basically, I need an input test to be able to be used in a calculator that will be inside the code, and then it needs to be placed in a TextView or a string so that the user can see the final answer.

Additional information: I have three EditText fields, and the user must fill in all three, then click "equals" and he will answer them, all the mathematical data and such needs should be in the code so that the user simply enters the information and does not actually calculate what- sometime. Thanks for the help, please ask me if you need more information.

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

I added the code that was suggested, but I still get the parseInt error message, and I still need to add a button to calculate the numbers entered in the EditText area. How can I do it? Sorry, I'm fair for 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); } ; } 
+7
source share
4 answers

First enter an instance of EditText:

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

Then enter the line that is currently displayed:

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

Then parse it for an integer:

 int myEditNum = Integer.parseInt(myEditValue); 
+12
source

In the EditText property, you must change the Input type . For numbers, you have three options: number , numberSigned or numberDecimal . It depends on what you want.

Then, if you want to convert your line in your EditText to an integer, you can do:

 int value = new Integer(myEditText.getText().toString()).intValue; 
+3
source
 EditText myEdit = (EditText) findViewById(R.id.edittext1); String myEditValue = myEdit.getText().toString(); int myEditNum = Integer.parseInt(myEditValue); myEditNum.setText(textOut1); 

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

I also tried (below) converting int myEditNum to String and then displaying it in a TextView, but still does not work, as I get the following error for the setText method: '' The setText (TextView) method is undefined for the String type. '' 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 is this happening and how to solve it?

+1
source

You should use:

 myEdit.setText(myEditValue); 

myEditValue is a string version of myEditNum .

+1
source

All Articles