旋转屏幕时,微调器的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) {}
});
}