Why doesn't my editText show the correct output in android?

I am starting to program in android and I am trying to write a simple calculator with 3 editText fields. 2 of them are used as inputs, and the 3rd - as a result of output. when I click the "+" button, I expect it to display a sum of two numbers. but instead, I get a strange number, for example, 2 + 3 = 2.131230724E9 I tried to solve the problem on the network, but could not find this problem ... thanks ...

final double tf1 = Double.parseDouble(String.valueOf(R.id.editText1)); final double tf2 = Double.parseDouble(String.valueOf(R.id.editText2)); Button btplus = (Button)findViewById(R.id.button1); btplus.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { double tf3; tf3 = tf1 + tf2; EditText result = (EditText)findViewById(R.id.editText3); result.setText(String.valueOf(tf3)); } }); 
+4
source share
5 answers

R.id.editText1 refers to the int value in R.java .

You need to initialize the EditText in onCreate . If you do not want the last modifier to declare EditText as a member of the class

  final EditText ed1 = (EditText) findViewById(R.id.editText1); final EditText ed2 = (EditText) findViewById(R.id.editText1); final EditText result = (EditText)findViewById(R.id.editText3); 

Then

  Button btplus = (Button)findViewById(R.id.button1); btplus.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { double tf3; double tf1 =Double.parseDouble(ed1.getText().toString()); double tf2 =Double.parseDouble(ed2.getText().toString()); tf3 = tf1 + tf2; result.setText(String.valueOf(tf3)); } }); 
+2
source

The problem is your code:

 final double tf1 = Double.parseDouble(String.valueOf(R.id.editText1)); final double tf2 = Double.parseDouble(String.valueOf(R.id.editText2)); 

you do not accept the value of EditText, but instead refer to objects. You should change this for something like this

 final double tf1 = Double.parseDouble(((EditText)findViewById(R.id.editText1)).getText().toString()); final double tf2 = Double.parseDouble(((EditText)findViewById(R.id.editText2)).getText().toString()); 

In addition, you must include these lines in the OnClick method of the button, so it will take the current values ​​when you click the button.

+2
source

Instead of this:

 result.setText(String.valueOf(tf3)); 

Use this:

 result.setText("" + tf3); 
0
source

Try the following:

 EditText editText1 = (EditText) findViewById(R.id.editText1); EditText editText2 = (EditText) findViewById(R.id.editText2); final double tf1 = Double.parseDouble(editText1.getText().toString()); final double tf2 = Double.parseDouble(editText2.getTExt().toString()); 

R.id is just a reference integer, so you get odd numbers when you add two integers id, and not in the actual value in the text box.

0
source
 final double tf1 = Double.parseDouble(edittext1.getText().toString()); final double tf2 = Double.parseDouble(edittext2.getText().toString()); Button btplus = (Button)findViewById(R.id.button1); btplus.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { double tf3 = 0; tf3 = tf1 + tf2; EditText result = (EditText)findViewById(R.id.editText3); result.setText(String.valueOf(tf3)); } }); 
0
source

Source: https://habr.com/ru/post/1494277/


All Articles