Domanda

Ehi, Sto cercando di ottenere la mia finestra editText per cancellare il suo contenuto quando: 1) Viene riselezionato per inserire dati diversi o 2) Viene premuto un pulsante per inserire dati predeterminati

Questo non è un suggerimento.Al momento, una volta inseriti i dati predeterminati, rimangono lì e non riesco a cancellarli, nemmeno usando cancella. Ho provato a inserire editBox.setText ("") in onClick Listener e addTextChangedListener (sia prima che sui metodi) utilizzando un booleano per determinare se il codice è già stato inserito, ma 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();
    }
}

Grazie

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top