setOnFocusChangeListener problem with edittext without filling

Asked

Viewed 11 times

0

someone could help me please.

I am with a problem, I made a setonfocus in my app, for it calculate and show the result as soon as the user click outside the edittext, however if the user does not type anything, the app closes and error in Integer.parserint someone here has already gone through it and could help me please. I wanted that if the user did not enter anything, the app continued.

precoEdittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus && precoEdittext.getText().toString() != null){
                    String tempEdittextqtd = qtdEdittext.getText().toString();
                    int qtdEdittextI = 0;
                    qtdEdittextI = Integer.parseInt(tempEdittextqtd);
                    String  tempEdittextprec = precoEdittext.getText( ).toString( );
                    int precoEdittextI = 0 ;
                    precoEdittextI = Integer.parseInt( tempEdittextprec );
                    resulEdittext = qtdEdittextI * precoEdittextI;
                    totalEdittext.setText(String.valueOf("R$"+resulEdittext));
                    Toast.makeText(getApplicationContext(),"TOTAL EDITTEXT "+totalEdittext.getText().toString(), Toast.LENGTH_SHORT).show();
                }
            }
        });

1 answer

0

The problem is that the content of EditText as empty string cannot be converted to an integer. An alternative you use is to assign the value zero if the text is empty.

int qtdEdittextI = 0;
int precoEdittextI = 0 ;
try {
    String tempEdittextqtd = qtdEdittext.getText().toString();
    qtdEdittextI = Integer.parseInt(tempEdittextqtd);
} catch (Exception e) { 
    qtdEdittext.setText("0")
}
try {
    String tempEdittextprec = precoEdittext.getText( ).toString( );
    precoEdittextI = Integer.parseInt( tempEdittextprec );
} catch (Exception e) { 
    precoEdittext.setText("0")
}

Browser other questions tagged

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