Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing previously entered text in EditText field

Hey, I'm trying to get my editText window to clear its contents when either: 1) It is reselected to enter different data or 2) A button is pressed to input predetermined data

This is not hint text. At the moment once the predetermined data is input it stays there and I can't clear it, even using delete. I have tried putting editBox.setText("") in the onClick Listener and the addTextChangedListener (both before and on methods) using a boolean to determine if code has already been input, 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();
    }
}

Thanks

like image 552
Broo Avatar asked May 05 '11 13:05

Broo


2 Answers

Errr...

The only code I use to achieve 1) is:

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

host just being an EditText. Works just fine? I think you are making this more complicated than it is.

like image 95
Codemonkey Avatar answered Sep 30 '22 13:09

Codemonkey


To clear When Clear Button is Clicked:

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

It Worked For me. Hope It Helps.

like image 28
ashim888 Avatar answered Sep 30 '22 13:09

ashim888