EditText - How to set default text in android edittext

I am working on an android application that people will enter into their accounts. I have an EditText that people enter the amount.

What I want to do is, for example, if a person enters "2", he should be automatically converted to "0.02". Then, if he wants to enter $ 22, he must press the buttons 2, 2, 0 and 0. It will be so "22.00". How do I do this using EditText. Can you give me a couple of guys?

Here are the related code snippets:

<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()) ; } 
+4
source share
5 answers
 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)); } }); 
+3
source

To do this, you need to use textWatcher as follows:

 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 } }); 
+2
source

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 } }); 
+1
source
 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); 
0
source

You are looking for a TextWatcher to change its input when it enters 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); 
0
source

Source: https://habr.com/ru/post/1494271/


All Articles