为什么在onNothingSelected的微调不会被调用? [英] why was the onNothingSelected in spinner not invoked?

查看:144
本文介绍了为什么在onNothingSelected的微调不会被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android微调,我要听的情况下,当用户preSS返回键的时候,微调的选择面板showing.I有落实OnItemSelectedListener,但onNothingSelected(适配器视图arg0中)没有被引用当preSS返回键。

I have an Android Spinner and I want to listen the event when the user press "Back Key" when the spinner's select panel is showing.I have implement the OnItemSelectedListener ,but the onNothingSelected(AdapterView arg0) was not invoked when press back key.

我只是想听听,当用户选择什么(或消失的选择面板)的事件。

I just want to listen the event when user select nothing( or the select panel disappear) .

有没有做这个正确的方式?

Is there a correct way to do this?

谢谢!

 Spinner s1 = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.colors, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s1.setAdapter(adapter);
    s1.setOnItemSelectedListener(
            new OnItemSelectedListener() {
                public void onItemSelected(
                        AdapterView<?> parent, View view, int position, long id) {
                    showToast("Spinner1: position=" + position + " id=" + id);
                }

                public void onNothingSelected(AdapterView<?> parent) {
                    showToast("Spinner1: unselected");
                }
            });


这是在Android 2.2的SDK的样本,它也没有表现出Spinner1:未选中当选择面板中消失


This is a sample in Android 2.2 SDK,it's also not show "Spinner1: unselected" when the select panel disappear.

推荐答案

看起来你将不能够做你想做的,而不扩展微调类。看来,微调没有一个 OnCancelListener AlertDialog 它建立显示的项目。

It looks like you won't be able to do what you want without extending the Spinner class. It seems that Spinner doesn't register an OnCancelListener with the AlertDialog it builds to display the items.

code <一个href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/widget/Spinner.java"相对=nofollow> Spinner.java :

  @Override
    public boolean performClick() {
        boolean handled = super.performClick();

        if (!handled) {
            handled = true;
            Context context = getContext();

            final DropDownAdapter adapter = new DropDownAdapter(getAdapter());

            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            if (mPrompt != null) {
                builder.setTitle(mPrompt);
            }
            mPopup = builder.setSingleChoiceItems(adapter, getSelectedItemPosition(), this).show();
        }

        return handled;
    }

    public void onClick(DialogInterface dialog, int which) {
        setSelection(which);
        dialog.dismiss();
        mPopup = null;
    }

此外, setSelection 在对话框中单击某项时只调用。当用户presses后退按钮,因为这是一个 OnCancel的事件这将不会被调用。

Also, setSelection is only called when an item in the dialog is clicked. This won't be called when the user presses the back button since that is an OnCancel event.

扩展微调将是一个有点痛,因为你要回适配器视图复制一切到源从自各成员的Andr​​oid源字段为实施只露在包级别。

Extending Spinner will be a bit of a pain since you have to copy everything back to AdapterView into your source from the android source since various member fields necessary for implementation are only exposed at the package level.

这篇关于为什么在onNothingSelected的微调不会被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆