Pregunta

From reading the Android documentation, I understand it is not possible to use addTextChangedListener for Spinner, but is there an alternative that I am missing?

I have created a number of forms and I would like to notify the user when there are unsaved changes (which I can do when I use EditText boxes).

Below is an example of the textChangeListener for EditText:

     inspectionReferenceEditText.addTextChangedListener(new TextWatcher(){
         public void afterTextChanged(Editable s) {
         }
         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
         }
         public void onTextChanged(CharSequence s, int start, int before, int count) {
             changesMade = true;
         }

Many thanks.

¿Fue útil?

Solución

Since a Spinner does not involve a user typing text, I don't see how a TextWatcher will help. However you can use an OnItemSelectedListener to be notified when the user makes a change.

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    int previous = -1;
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        if(previous != position && previous < -1) {
            Log.v("Example", "Item Selected: " + parent.getItemAtPosition(position).toString());
            // Do something
        }
        previous = position;
    }

    public void onNothingSelected(AdapterView<?> parent) {}
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top