-
隔江千里
以前的一些答案是不正确的。它们适用于其他小部件和视图,但用于旋转小部件明确指出:旋转器不支持项目单击事件。调用此方法将引发异常。更好地利用OnItemSelectedListener()相反:spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}});这对我有用。注意,在构建视图时也会调用onItemSelected方法,因此可以考虑将其放入其中onCreate()方法呼叫。
-
LEATH
Spinner spnLocale = (Spinner)findViewById(R.id.spnLocale);spnLocale.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
// Your code here
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
} });注:记住一件事。纺纱机OnItemSelectedListener事件将执行两次:旋转初始化用户手动选择尝试使用标志变量来区分这两者。
-
吃鸡游戏
你可以实现AdapterView.OnItemSelectedListener在你的活动中上课。然后在onCreate()Spinner spin = (Spinner) findViewById(R.id.spinner);spin.setOnItemSelectedListener(this);然后重写这两个方法:public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
selection.setText(items[position]);}public void onNothingSelected(AdapterView<?> parent) {
selection.setText("");}