How do I set a value in Edittext if empty?

Asked

Viewed 413 times

0

EditText edit = (EditText)findViewByid(R.id.edit); //estabeleço o conexão

if(edit.getText().toString().isEmpty()){ // verificando se a edit esta vazia

String num = "6"; // Ou use um int, não vejo necessidade de ser um double

edit.setText(num); //inserindo valor na edit
}

//above code works

//this one not below

//method that calls for the class public class Temperature {

public double getFaRetorneCels(double cel){
    return  ((cel*9)/5)+32;
}

public double getCelRetorneFa(double fah){
    return ((fah-32)*5/9);
}

}

// button code

public void Convert(View view){ EditText edtcel = (EditText)findViewById(R.id.edtCel); Edittext edtfa = (Edittext)findViewById(R.id.edifa);

    Temperatura t = new Temperatura();

     double c = Double.parseDouble(edtcel.getText().toString());
     double f = Double.parseDouble(edtfa.getText().toString());

    if(edtcel.getText().toString().isEmpty() && edtfa.getText().toString().isEmpty() ){
        Toast.makeText(this,"Pelo menos um campo deve ser preenchido!!!",Toast.LENGTH_SHORT).show();
    }else if(edtcel.getText().toString().isEmpty()) {

        edtcel.setText(String.valueOf(t.getCelRetorneFa(f)));

    }else  if (edtfa.toString().isEmpty()){

        edtfa.setText(String.valueOf(t.getFaRetorneCels(c)));


    }

If anyone can help, thank you

1 answer

0

Do it like this:

EditText edit = (EditText)findViewByid(R.id.edit); //estabeleço o conexão

if(edit.getText().toString().isEmpty()){ // verificando se a edit esta vazia

String num = "6"; // Ou use um int, não vejo necessidade de ser um double

edit.setText(num); //inserindo valor na edit
}
  • I managed to find the solution, Thanks to him help @Woton Sampaio the problem was in double because I was assigning her value based by Edit ignoring that one of the Edit would come empty, Just put the double within the condition of the if and worked worked normal rode without problems thanks for the help.

Browser other questions tagged

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