When I delete the last character from EditText's app, what do I do?

2

I type a number sequence normally and talz, now if for example I type "1234" in any of the 3 EditTexts existing in my Activity ...

...anddeletethelastcharacter,thatis,whenIdeletethelastremainingnumber(inthiscasethe"1" ), the app for:

I would like the problem to be solved in order to make the number 0 a mandatory character, that is, if it has nothing entered by the user, 0 appears in EditText and also if it has something, and then everything is deleted, it resumes , so even if the user tries to delete that number 0 it stays strong and strong. p>

Main01Activity:

package genesysgeneration.ruleoftree;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Main01Activity extends AppCompatActivity {

    private EditText et01, et02, et03;
    private TextView tv01;
    private long l01, l02, l03;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main01);

        l01=0;
        l02=0;
        l03=0;

        tv01=(TextView)findViewById(R.id.tv01);

        et01=(EditText)findViewById(R.id.et01);
        et02=(EditText)findViewById(R.id.et02);
        et03=(EditText)findViewById(R.id.et03);

        addValuesLong();

    }

    private void addValuesLong(){

        et01.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) {

                l01=Long.parseLong(et01.getText().toString().trim());
                tv01.setText(String.valueOf(l01*l02*l03));

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        et02.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) {

                l02=Long.parseLong(et02.getText().toString().trim());
                tv01.setText(String.valueOf(l01*l02*l03));

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        et03.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) {

                l03=Long.parseLong(et03.getText().toString().trim());
                tv01.setText(String.valueOf(l01*l02*l03));

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

}
    
asked by anonymous 31.01.2017 / 01:01

2 answers

2

To resolve this problem, a possible workaround is to create a condition to compare the size of CharSequence and perform any procedure if it is greater than 0 . See:

if (s.length() > 0) {

    // realize o procedimento
} 

So, it would look like this:

et01.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) {

        if (s.length() > 0) {
            l01 = Long.parseLong(et01.getText().toString().trim());
            tv01.setText(String.valueOf(l01 * l02 * l03));
        } else {
            l01 = 0;
            et01.setHint("0");
            tv01.setText(String.valueOf(l01*l02*l03));
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

Note: It is necessary to make the condition all EditText 's being used for this purpose.

    
31.01.2017 / 23:01
1

Instead of setting the text to 0 when empty, use hint for this:

editText.setHint("0");

Now when you get the number, you have to check if it is the virtual zero (no input) or text:

long valor;
String text = editText.getText().toString();
if (text.isEmpty()) {
    valor = 0.0f;
} else {
    valor = Long.parseLong(text);
}

So when it is empty, 0 will appear in the background. If you need to change the hint color to look like the input text.

    
31.01.2017 / 01:24