【问题标题】:why was the onNothingSelected in spinner not invoked?为什么没有调用微调器中的 onNothingSelected?
【发布时间】:2011-03-29 06:59:16
【问题描述】:

我有一个 Android 微调器,当微调器的选择面板显示时,我想在用户按下“返回键”时监听事件。我已经实现了 OnItemSelectedListener ,但是当按下返回时没有调用 onNothingSelected(AdapterView arg0)键。

我只想在用户什么都不选择(或选择面板消失)时监听事件。

有正确的方法吗?

谢谢!


 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");
                }
            });

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

【问题讨论】:

    标签: android spinner


    【解决方案1】:

    如果不扩展 Spinner 类,您将无法做您想做的事。似乎Spinner 没有使用它为显示项目而构建的AlertDialog 注册OnCancelListener

    来自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 仅在单击对话框中的项目时调用。当用户按下返回按钮时不会调用它,因为这是一个OnCancel 事件。

    扩展Spinner 会有点痛苦,因为您必须将所有内容从android 源复制回AdapterView 到您的源中,因为实现所需的各种成员字段仅在包级别公开。

    【讨论】:

    • 真诚感谢 Qberticus!现在看来是唯一的办法了。
    • @Qberticus 我想我不明白什么时候调用过 onNothingSelect ?如果是现在什么都不选,那么什么时候?
    【解决方案2】:

    另一种方法是创建一个最小的自定义微调器下拉项,即:

    <com.mypackage.MyTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        style="?android:attr/spinnerDropDownItemStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    
        android:textSize="25dp"
     />
    

    然后拦截onDetachedFromWindow():

    public class MyTextView extends TextView {
    
        public MyTextView(Context context) {
            super(context);
        }
    
        public MyTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MyTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onDetachedFromWindow() {
            super.onDetachedFromWindow();
            // Callback here
        }
    }
    

    如果您使用自定义 ArrayAdapter 仅设置一个下拉项来执行回调,以及设置 回调等合适的上下文。

    根据您在回调中执行的操作,您可能希望 将其发布为可运行文件,以便完全清理微调器 在它做任何事情之前。

    【讨论】:

      猜你喜欢
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      • 2022-11-09
      • 1970-01-01
      • 1970-01-01
      • 2013-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多