1
I own 3 EditText
.
In the first I put the quantity and in the second a value.
I needed that on the 3rd EditText
instantly present the result of the operation.
You can do this on Android without using javascript?
1
I own 3 EditText
.
In the first I put the quantity and in the second a value.
I needed that on the 3rd EditText
instantly present the result of the operation.
You can do this on Android without using javascript?
0
For this you must use TextWatcher
:
The example below concatenates the two String in the third Edittext:
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(!"".equals(editText1.getText().toString()) && !"".equals(editText2.getText().toString())){
final String concat = editText1.getText().toString()+" : "+editText2.getText().toString();
editText3.setText(concat);
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
editText1.addTextChangedListener(textWatcher);
editText2.addTextChangedListener(textWatcher);
Click here for documentation
I hope I’ve helped!
Greetings!
I’ll try, man, fight!!!
If it works, don’t forget to mark the answer as correct! Greetings!
Valew! greetings!!!
0
You should use a Textwatcher for this.
Below is a detailed example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/edittext_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_price"
android:inputType="number" />
<EditText
android:id="@+id/edittext_quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_quantity"
android:inputType="number" />
<EditText
android:id="@+id/edittext_amount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_amount"
android:inputType="number" />
</LinearLayout>
Your Victoria would look like this:
public class MainActivity extends Activity {
EditText editTextPrice;
EditText editTextQuantity;
EditText editTextAmount;
double price, quantity, amount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextPrice = (EditText) findViewById(R.id.edittext_price);
editTextQuantity = (EditText) findViewById(R.id.edittext_quantity);
editTextAmount = (EditText) findViewById(R.id.edittext_amount);
editTextPrice.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.length() > 0) {
price = Double.parseDouble(s.toString());
}
amount = price * quantity;
editTextAmount.setText(String.format("%s", String.valueOf(amount)));
}
});
editTextQuantity.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.length() > 0) {
quantity = Double.parseDouble(s.toString());
}
amount = price * quantity;
Locale fmtLocale = Locale.getDefault();
NumberFormat formatter = NumberFormat.getInstance(fmtLocale);
formatter.setMaximumFractionDigits(2);
editTextAmount.setText(String.format("R$ %s", formatter.format(amount)));
}
});
}
}
Don’t forget to set in your XML layout the inputType of each Edittext to avoid conversion errors.
Valew man, that’s exactly what I needed!
Nothing, man. Always happy to help!
Browser other questions tagged android
You are not signed in. Login or sign up in order to post.
Javascript? Don’t mean Java?
– ramaral
I will need to do this on Android, I know that with Javascript this works because I already did this, but I wanted to avoid using Javascript on Android. I think you think I’m using xml to do this, but no, I’m doing everything in java even dynamically.
– daniel12345smith
You are developing a hybrid or Android native SDK application?
– Rodrigo Henriques
I’m using native SDK
– daniel12345smith