Deleting previously entered text in the EditText field

Hey, I'm trying to get the editText window to clear its contents when: 1) It is re-selected to enter different data or 2) A button is pressed to enter predefined data.

This is not a hint text. At the moment, when the predefined data is entered, it remains there, and I cannot clear it, even using delete. I tried putting editBox.setText ("") in the onClick Listener and addTextChangedListener (both before and on the methods), using a boolean to determine if the code was already entered, but it just ignores it.

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();
    }
}

thank

+5
source share
3 answers

Errr ...

, 1):

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

host EditText. ? , , .

+7

"":

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("");
    }
});

. , .

+3

For 1) onClick did not work for me, since I had to double click to call onClickListener. Another approach would be setOnFocusChangeListener ()

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

For 2) the solution given by ashim888 works well.

0
source

All Articles