Answer the question
In order to leave comments, you need to log in
How to make calculation of three TextView?
Hello!
Please tell me how to calculate the three parameters. I only delve into this for a week ...
There is an EditText and a button to it.
And there are three TextViews that get data from SQLLite.
I'm looking for examples of how to multiply EditText by TextView
(TextView - @+id/date) news_date - this is how much sugar has risen (12)
(TextView - @+id/insulin) news_insulin - how much insulin was injected (4)
(TextView - @+id/ uglevodi) news_uglevodi - how many carbohydrates per 100 grams (50)
In the EditText I enter how much I eat, for example 400 grams, and you need 400*12/100=48 and replace the existing TextView, etc.
I found an example of finding a TextView and replacing, but I can’t find an example of multiplication.
public void MyFunc(View view) {
// тут расчет X, Y, Z
// находим поля для ввода
TextView date = (TextView) findViewById(R.id.date);
TextView insulin = (TextView) findViewById(R.id.insulin);
TextView uglevodi = (TextView) findViewById(R.id.uglevodi);
// заменяем текст
date.setText(String.valueOf(X));
insulin.setText(String.valueOf(Y));
uglevodi.setText(String.valueOf(Z));
}
Answer the question
In order to leave comments, you need to log in
Is it really harder to multiply variables than to wedge a base into a program?
You need a logic processing point for a button (Button) or a listener on editText.
Here with a button
You create a button, connect it to an xml button, hang a listener on it (setOnClicktListener in general, this is a button method), where you process the logic.
in the same place you output the textView.setText(Integer.valueOf(getText().toString * Integer.valueOf(ObjectNameEditText.getText().toString())) Here the value of the EditText field is simply multiplied by the value of the TextView and stored in the TextView and so on for your 3 texView
And now you impudently click on the button and the changes will take effect
Or you can hang it on editText and when you press enter there, for example, the changes will take effect here is the link there are many different listenershttps://dolbodub.blogspot.com/2013/03/edittext.html
If suddenly you can’t do it, I will personally write you a code with explanations
TextView is not just some value, it's an object that, in addition to its value, has screen coordinates, dimensions, and a bunch of other parameters, so you can't just multiply it.
With the setText method, you have set a new value that will be displayed by the TextViev.
With the getText() method, you can get its value, but in the form of a CharSequence that you can convert to a String. But you can't do math with a string, so you need to convert it to a numeric value, like Integer:
Integer.valueOf(insulin.getText().toString());
But, it's not right to do so. The data must be stored separately from the display. You should have variables in your code that have the numeric types you need, and the TextView only displays the values of these variables. Then, to perform the calculations you need, or to save the values, you will not need to get the values from the TextView, they will already be available to you in the form of your variables.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question