Android Spinner: Get the selected item change event

前端 未结 16 2355
挽巷
挽巷 2020-11-22 16:47

How can you set the event listener for a Spinner when the selected item changes?

Basically what I am trying to do is something similar to this:

spinn         


        
16条回答
  •  时光取名叫无心
    2020-11-22 17:34

    You can implement AdapterView.OnItemSelectedListener class in your Activity.

    And then use the below line within onCreate()

    Spinner spin = (Spinner) findViewById(R.id.spinner);
    spin.setOnItemSelectedListener(this);
    

    Then override these two methods:

    public void onItemSelected(AdapterView parent, View v, int position, long id) {
        selection.setText(items[position]);
    }
    
    public void onNothingSelected(AdapterView parent) {
        selection.setText("");
    }
    

提交回复
热议问题