1

旋转屏幕时,微调器的 OnItemSelected 事件似乎触发了两次。为什么这样做?我需要它只触发一次(因为它必须触发一次)

int counter = 0;
protected void onCreate(Bundle savedInstanceState) {
    Spinner spinner_1 = (Spinner)findViewById(R.id.spinner_1);
    spinner_1.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {   
            counter++;
            if (counter>1)
                Toast.makeText(getApplicationContext(), String.valueOf(counter), Toast.LENGTH_SHORT).show(); 
                // always outputs 2 when I rotate the phone
        }
        @Override
        public void onNothingSelected(AdapterView<?> parentView) {}
    });
}
4

0 回答 0