当Spinner弹出对话框关闭时如何获取回调?

7
我有一个安卓的Spinner,希望在用户从弹出对话框中选择某项时获得回调。看起来setOnItemClickListener()或者setOnItemSelectedListener()是正确的方法,但是当我在spinner中选择一项时,它们都没有被调用。
是否有正确的方法可以做到这一点?
更新:
根据commonsware的建议,我做了以下操作,但我的onItemSelected()方法从未被调用:
    final Spinner spinner = (Spinner) findViewById(R.id.spinner);

    spinner.setAdapter( new ArrayAdapter<SettingValue>(getContext(), android.R.layout.simple_list_item_1, android.R.id.text1, setting.getSettingValues() ) );
    spinner.setOnItemSelectedListener( new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Log.d("BOOGA");
            final SettingValue settingValue = (SettingValue)parent.getSelectedItem();
            final Editor edit = getContext().getSharedPreferences( PREFS_CONTEXT_NAME, Context.MODE_PRIVATE).edit();
            edit.putString(setting.name(), settingValue.name());
            edit.commit();
        }

        public void onNothingSelected(AdapterView<?> parent) {
            // do nothing
        }

    });
1个回答

1
使用 setOnItemSelectedListener() 方法。以下是我一本 书籍样例
注:link1 需要添加超链接到相关书籍。
/***
    Copyright (c) 2008-2009 CommonsWare, LLC

    Licensed under the Apache License, Version 2.0 (the "License"); you may
    not use this file except in compliance with the License. You may obtain
    a copy of the License at
        http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

package com.commonsware.android.selection;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class SpinnerDemo extends Activity
    implements AdapterView.OnItemSelectedListener {
    TextView selection;
    String[] items={"lorem", "ipsum", "dolor", "sit", "amet",
                    "consectetuer", "adipiscing", "elit", "morbi", "vel",
                    "ligula", "vitae", "arcu", "aliquet", "mollis",
                    "etiam", "vel", "erat", "placerat", "ante",
                    "porttitor", "sodales", "pellentesque", "augue", "purus"};

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        selection=(TextView)findViewById(R.id.selection);

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

        ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);

        aa.setDropDownViewResource(
            android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(aa);
    }

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

    public void onNothingSelected(AdapterView<?> parent) {
        selection.setText("");
    }
}

1
感谢那段代码,commonsware。我试过了但对我来说没起作用。onItemSelected()监听器没有被调用,不知道为什么。请看我上面的更新。有什么想法吗? - emmby
你根本没有使用我的代码。如果你对比这两个版本,你会发现在ArrayAdapter构造函数的第二个参数中你没有使用Spinner资源,并且你也没有向适配器提供下拉资源。修复这两个问题,看看是否解决了你的问题。如果没有,就寻找其他差异。 - CommonsWare
11
这段代码很有帮助,但它没有考虑当用户“取消”下拉框(即触摸下拉框外部以关闭它)的情况。本地Spinner代码存在一个主要缺陷——没有弹出窗口或对话框打开或关闭的监听器或回调事件。例如,基于弹出窗口的打开或关闭更改下拉框标题(可能更改箭头或其他内容)似乎是不可能的。 - Batdude

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接