How do you do mathematical action?
-
I've got two EditText, I've got to do the mathematical action of the 1EditText, 2EditText, and I can multiply 2
binding.inputQuantity binding.inputPrice
Found an example.
int a = Integer.parseInt(binding.inputQuantity.getText().toString()); int b = Integer.parseInt(binding.inputPrice.getText().toString());
int res = (a * b) * 2;
binding.totalPrice.setText(String.valueOf(res));
But it works when EditText originally added some text.
android:text="1"
+ Also, there is nothing to do with re-entry. I need it to be upgraded with an induction at EditText without any compression processors.<EditText
android:id="@+id/inputQuantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="4dp"
android:background="@drawable/input_bg"
android:drawableStart="@drawable/ic_view"
android:drawablePadding="10dp"
android:ems="10"
android:fontFamily="@font/aldrich"
android:text="1"
android:hint="@string/_1"
android:inputType="number"
android:paddingStart="20dp"
android:paddingTop="13dp"
android:paddingEnd="20dp"
android:paddingBottom="13dp"
android:textColor="@color/colorWhite"
android:textColorHint="@color/colorWhite"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/inputTitle"
app:layout_editor_absoluteX="97dp"
app:layout_editor_absoluteY="248dp" /><EditText android:id="@+id/inputPrice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginStart="4dp" android:background="@drawable/input_bg" android:drawableStart="@drawable/ic_price" android:drawablePadding="10dp" android:ems="10" android:fontFamily="@font/aldrich" android:text="1" android:hint="@string/_2" android:inputType="number" android:paddingStart="20dp" android:paddingTop="13dp" android:paddingEnd="20dp" android:paddingBottom="13dp" android:textColor="@color/colorWhite" android:textColorHint="@color/colorWhite" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/inputTitle" app:layout_editor_absoluteX="97dp" app:layout_editor_absoluteY="248dp" />
Found what I need to do, but only when, from any field of EditText, the app closes because the field is empty, how can it be corrected?
binding.inputPrice.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) { int a = Integer.parseInt(binding.inputQuantity.getText().toString()); int b = Integer.parseInt(binding.inputPrice.getText().toString()); int res = (a * b) * 2; binding.totalPrice.setText(String.valueOf(res)); } public void beforeTextChanged(CharSequence s, int start, int count, int after) {} public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
binding.inputQuantity.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) { int a = Integer.parseInt(binding.inputQuantity.getText().toString()); int b = Integer.parseInt(binding.inputPrice.getText().toString()); int res = (a * b) * 2; binding.totalPrice.setText(String.valueOf(res)); } public void beforeTextChanged(CharSequence s, int start, int count, int after) {} public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
-
That's what helped me solve this problem, namely the elimination of tey end catch!
private void gettingTotalPrice() {
binding.inputPrice.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { try { int a = Integer.parseInt(binding.inputQuantity.getText().toString()); int b = Integer.parseInt(binding.inputPrice.getText().toString()); int res = (a * b) * 2; binding.totalPrice.setText(String.valueOf(res)); } catch (NumberFormatException e) { e.printStackTrace(); } } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { } }); binding.inputQuantity.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { try { int a = Integer.parseInt(binding.inputQuantity.getText().toString()); int b = Integer.parseInt(binding.inputPrice.getText().toString()); int res = (a * b) * 2; binding.totalPrice.setText(String.valueOf(res)); } catch (NumberFormatException e) { e.printStackTrace(); } } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { } });
}