为什么没有调用spinner中的onNothingSelected?

时间:2010-08-13 05:01:59

标签: android spinner

我有一个Android Spinner,当用户在显示微调器的选择面板时按“Back Key”时我想听这个事件。我已经实现了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");
                }
            });

这是Android 2.2 SDK中的示例,当选择面板消失时,它也不会显示“Spinner1:unselected”。

2 个答案:

答案 0 :(得分:3)

如果不延长Spinner课程,您似乎无法做到你想要的。似乎Spinner未向OnCancelListener注册AlertDialog以显示项目。

来自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到源代码,因为实现所需的各个成员字段只在包级别公开

答案 1 :(得分:0)

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

<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仅设置其中一个下拉项来执行回调,以及设置,则可以对此进行精细处理 适用于回调的上下文等。

根据你在回调中的操作,你可能想要 将其作为可运行的方式发布,以便完全清理微调器 在它做任何事之前。