How to clean a field from a textView?

2

Hello in the code I am using a EditText and several TextView , the point is that I am trying that when the value of EditText this empty clean the fields of TextView .

That is the code with which I am trying it

TextView tex1 , tex2 , tex3 ,tex4 , tex5, tex6 , tex7 , tex8;

     EditText editText ;

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

        tex1 = (TextView)findViewById(R.id.tex1longitud);
        tex2 = (TextView)findViewById(R.id.tex2longitud);
        tex3 = (TextView)findViewById(R.id.tex3longitud);
        tex4 = (TextView)findViewById(R.id.tex4longitud);
        final String clear = new String("");

       final EditText editText = (EditText)findViewById(R.id.edit1ejemplo);
        editText.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) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (!(editText.getText().toString()).isEmpty()) {

                    double valor = Double.parseDouble(editText.getText().toString());

                    if (valor >=0){
                    double resu1 = valor / 0.3048;

                    tex1.setText("Ft=" + resu1);


                    double resu2 = valor / 0.0254;

                    tex2.setText("inches" + resu2);

                    double resu3 = valor / 1000;

                    tex3.setText("Km=" + resu3);

                    double resu4 = valor * 1609.44;

                    tex4.setText("Milles=" + resu4);

                    }



                         if (valor  <=0){


                            tex1.setText("");

                             tex2.setText("");

                             tex3.setText("");

                              tex4.setText("");


                    }

                }
    
asked by Liantony Pozo 22.04.2017 в 02:14
source

2 answers

3

Try this:

    if (!(editText.getText().toString()).isEmpty()) {
           // Tu código tal como está
         }
    else{
        tex1.setText("");
        tex2.setText("");
        tex3.setText("");
        tex4.setText("");
   }
    
answered by 22.04.2017 / 02:23
source
1

The best thing to do would be to determine if it is empty in EditText and delete the texts in TextView :

if((editText.getText().toString).isEmpty()){ //EditText vacìo.

 tex1.setText();
 tex2.setText();
 tex3.setText();
 tex4.setText();

}

Applying this to your code would be:

if(!(editText.getText().toString).isEmpty()){ //si EditText no esta vacìo.

   //realiza operaciones.

    }
else{ //vacia TextViews.
      tex1.setText();
     tex2.setText();
     tex3.setText();
     tex4.setText();
}
    
answered by 22.04.2017 в 04:08