Answer the question
In order to leave comments, you need to log in
How to convert currencies with calculation for any field?
The code is taken as a basis and partially reworked by me. While I'm reading a textbook on Java, I imagine what should happen and try to learn something in practice. It was possible to get exchange rates by api. There are 4 (actually 5) but important 4 TextViews and 4 currency label search spinners. Date button to select exchange rates by date.
The draft version works fine, except for one thing - the calculation is made by entering the amount of the base currency in a specific field, but I would like to enter the amount in any field with the calculation for other fields. Here's a question for connoisseurs how, within the framework of the existing code, this could be implemented
public void currencyInputs() {
et1 = (EditText) findViewById(R.id.input1); //это
et2 = (EditText) findViewById(R.id.input2); //те
et3 = (EditText) findViewById(R.id.input3); //4
et4 = (EditText) findViewById(R.id.input4); //поля
et5 = (EditText) findViewById(R.id.basicValue); //тут еще расчет количества базовых величин (чисто белорусское изобретение)
etArray.add(et1); //тут ссылки передаем в массив
etArray.add(et2);
etArray.add(et3);
etArray.add(et4);
}
private void exchange() {
EditText baseEt = (EditText) etArray.get(0); // берется первое поле из массива за базовое
if (baseEt == null || TextUtils.isEmpty(baseEt.toString())) {
Log.e("EditText Error", "EditText Missing");
return;
}
double baseValue = (Double) Double.valueOf(baseEt.getText().toString());
//получаем из него содержимое и передаем в цикл и формулу
for (int i = 1; i < 4; i++) {
double convertedValue = baseValue / rate[i]; //rate[i] это массив переменных из курсов валют, которые выбраны в спиннерах
etArray.get(i).setText(String.valueOf(convertedValue));
}
double baseVal = baseValue / 29.0;
et5.setText(String.valueOf(baseVal));
}
Answer the question
In order to leave comments, you need to log in
Good afternoon!
The implementation logic is approximately the following:
you need some event listener that tracks field changes. If one of the fields has been changed, then we take the value of this field and get the type (currency) of this field. And then we send a request via the API and get the corresponding quotes.
andrej3337 ,
can a separate listener for each field do
A listener hung on them - all of the 4 fields were written to the log from it
for (int i = 1; i < 4; i++) {
Here is a listener hung for each field
private void getOnFocus0() {
etArray.get(0).setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
EditText baseEt = (EditText) etArray.get(0);
Log.e("id0", ""+baseEt);
}
}
});
}
E/id0: androidx.appcompat.widget.AppCompatEditText{51f7120 VFED..CL. .FP.ID 315.1-889.167 #7f0900e0 app:id/input1}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question