When I delete the last Edittext character from the PARA app, what to do?

Asked

Viewed 212 times

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...

inserir a descrição da imagem aqui

... and delete until the last character, ie when I delete the last remaining number (in case the "1"), the app for:

inserir a descrição da imagem aqui

I would like the problem to be solved in order to make the number 0 a mandatory character, that is, if you have nothing entered by the user, number 0 appears in Edittext and also if you have something, and then it’s all erased, he reappears, so that even if the user tries to delete that number 0 he stands firm and strong.

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

            }
        });

    }

}
  • of your code?

2 answers

2


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

if (s.length() > 0) {

    // realize o procedimento
} 

Then I’d stay that way:

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

    }
});

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

  • @puppet-symphores worked right there?

  • simsim, now I will solve the stop of textview ki nn tah displaying broken number, vlw aew!!!

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 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, a 0 will appear at the bottom. If you need to change the color of the hint to look like the input text.

  • I’m using long instead of double, just replace neh?

  • Yes, use long valor and Long.parseLong(text)

  • put that inside addTextChangedListener? pq out the app keeps stopping working.

  • colokei in, out, with and without Isis, nothing. tah giving everything in the same.

  • Yes, inside the addTextChangedListener.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.