I am working on an android application which people will enter their bills. I have an EditText which people enter the amount.
What I want to do is that for example If a person enters "2" it should be converted automatically to "0.02". Then if he/she wants to enter 22$ he should push 2, 2, 0 and 0 buttons. It will be like that "22.00". How can I manage to do that using EditText. Can you give me some ideas guys?
Here is the related code pieces :
<EditText
android:id="@+id/amount"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="5dip"
android:inputType="number"
android:hint="@string/enter_amount"
android:singleLine="true" />
EditText payment = (EditText) findViewById(R.id.amount);
if (payment.getText().toString().length() > 0){
total_amount =Integer.parseInt(payment.getText().toString()) ;
}
EditText payment = (EditText) findViewById(R.id.amount);
payment.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
total_amount =Integer.parseInt(s) ;
payment.setText(""+(total_amount/100));
}
});
For that you have to use textWatcher like this :
payment.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
use addTextChangedListener()
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
EditText payment = (EditText) findViewById(R.id.amount);
if (payment.getText().toString().length() > 0){
double total_amount =Double.parseDouble(payment.getText().toString()) ;
String amm = String.valueOf(total_amount/100);
payment.setText(amm);
You are looking for the TextWatcher
in order to alter their input as they enter it. Here is an example:
TextWatcher paymentWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
//Do your calculations here
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
};
payment.addTextChangedListener(paymentWatcher);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.