我的 Android 应用程序中有一个微调器,它的 onItemSelected()
事件会在进入 Activity 时自动触发。
如何避免这种情况?
最佳答案
我们可以使用一个标志,并在真正触摸微调器时启用它。
private boolean isSpinnerTouched = false;
spinner.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
isSpinnerTouched = true;
return false;
}
});
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View arg1,
int arg2, long arg3) {
if (!isSpinnerTouched) return;
// do what you want
}
});
关于android - 进入 Activity 时自动选择微调项。我该如何避免这种情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5368225/