Pregunta

Oye, Estoy tratando de que mi ventana editText borre su contenido cuando: 1) Se vuelve a seleccionar para ingresar datos diferentes o 2) Se presiona un botón para ingresar datos predeterminados

Este no es un texto de sugerencia.Por el momento, una vez que se ingresan los datos predeterminados, permanecen allí y no puedo borrarlos, incluso usando eliminar. He intentado poner editBox.setText ("") en el onClick Listener y el addTextChangedListener (tanto antes como en los métodos) usando un booleano para determinar si el código ya se ingresó, pero simplemente lo ignora.

foodBox.addTextChangedListener(new TextWatcher(){

        @Override
        public void afterTextChanged(Editable s) {

        }
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            addFood();
            valueAdded=true;
        }           
    });


    dogButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (valueAdded==true){
                foodBox.setText("");
                setFood(0.0);
            }
            isClicked=true;
            animal=1;
            addFood();
            valueAdded=true;//value in the box
                }


    });


    catButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (valueAdded){
               foodBox.setText("");}
            isClicked=true;
            animal=2;
            addFood();
            valueAdded=true;
            }

    });
            private void addFood() {
    try{
        aClass aob = new aClass();
        setFood(0.0);
        String a = this.foodBox.getText().toString();
        if(isClicked&&TextUtils.isEmpty(a)){
            if(animal==1){
                a=mob.getDog(); 
            }
            if(animal==2){
                a=mob.getCat();
            }
            this.foodBox.setText(a);
            double d=Double.parseDouble(a);
            setFood(d);
        }else{
            if(TextUtils.isEmpty(a)){
            a="0";
        }
        double d=Double.parseDouble(a);
        setFood(d);
        }
    } 
        catch (Exception e){
        this.showAnswer.setText("Please input animals in numbers");
        addFood();
    }
}

Gracias

¿Fue útil?

Solución

Errr ...

El único código que utilizo para lograr 1) es:

    // Clear text when clicked
    host.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            host.setText("");
        }
    });

host simplemente siendo un EditText.¿Funciona bien?Creo que lo está haciendo más complicado de lo que es.

Otros consejos

Para borrar cuando se hace clic en el botón Borrar:

firstName = (EditText) findViewById(R.id.firstName);

clear = (Button) findViewById(R.id.clearsearchSubmit);

clear.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.clearsearchSubmit);
        firstName.setText("");
    }
});

Funcionó para mí.Espero que te ayude.

For 1) onClick did not work for me as I had to press twice to call onClickListener. Other approach would be setOnFocusChangeListener()

editTextView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    editTextView.getText().clear();
                }
            }
        });

For 2) solution given by ashim888 works well.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top