2

我尝试将 on select 事件附加到我的 Androidandroid.widget.Spinner

我知道Spinner.setOnItemSelectedListener存在但不能按我的意愿工作,问题setOnItemSelectedListener是在使用适配器加载微调器时调用方法,并且选择预定义项目。

我想要一个界面来获取事件,而“用户”通过使用触摸屏从 Spinner 中选择一个项目。还有一个事件,仅在使用选择项目而不是程序选择项目时调用。

我想要一个在使用从这个微调器对话框中选择一个项目时调用的事件。

在此处输入图像描述

4

3 回答 3

3

通过查看http://www.oschina.net/code/explore/android-4.0.1/core/java/android/widget/Spinner.java上的代码,我终于找到了适合我的解决方案

我发现了如何防止或将更多代码附加到这个运行的代码部分,而使用将显示下拉列表,然后创建我的一个 Spinner 实例。

我的微调器类有一个使用定义接口OnSelectSpinnerListener,当用户触摸微调器然后从微调器列表中选择一个项目时,该接口将运行。

/**
 * Create a DropDown Spinner/Select List, that also has some event for detect
 * action do by the use and action do by the system.
 *
 * Use {@link OnSelectSpinnerListener} for attach a listener to this
 * SelectSpinner.
 */
public class SelectSpinner extends Spinner {

    public interface OnSelectSpinnerListener {

        /**
         * Call before the drop down menu is show that allow use to select one
         * of the item from the Spinner.
         */
        public void onOpenSelectMenu();

        /**
         * Call while the use press a button or item for select this. This will
         * not be call while the system set a select value for the spinner. Use {@link SelectSpinner#setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener)}
         * for get all event while a item is set to the spinner.
         *
         * @param index Select item as Index
         * @param object Select item as Object
         */
        public void onItemSelectByUser(int index, Object object);
    }
    private OnSelectSpinnerListener onSelectSpinnerListener;

    public SelectSpinner(Context context) {
        super(context);

    }

    public SelectSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SelectSpinner(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public OnSelectSpinnerListener getOnSelectSpinnerListener() {
        return onSelectSpinnerListener;
    }

    public void setOnSelectSpinnerListener(OnSelectSpinnerListener onSelectSpinnerListener) {
        this.onSelectSpinnerListener = onSelectSpinnerListener;
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        super.onClick(dialog, which);
        if (this.onSelectSpinnerListener != null) {
            Object item = super.getItemAtPosition(which);
            this.onSelectSpinnerListener.onItemSelectByUser(which, item);
        }
    }

    @Override
    public boolean performClick() {
        if (this.onSelectSpinnerListener != null) {
            this.onSelectSpinnerListener.onOpenSelectMenu();
        }
        return super.performClick();
    }
}
于 2012-10-30T17:00:56.873 回答
3

正如在件事中提到的那样,onClick()开始时不起作用,但最近版本的 android (我在 4.0 的某些版本上对其进行了测试)。

所以我为这个在较新版本的android中工作的FIG-GHD742调整了答案(在4.1和4.3上测试):

public class SelectSpinner extends Spinner {

    public interface OnSelectSpinnerListener {

        public void onOpenSelectMenu();

        public void onItemSelectByUser(AdapterView<?> parent, View view,
                int position, long id);
    }

    private boolean isUserOpen = false;

    private List<OnSelectSpinnerListener> onSelectSpinnerListeners;
    private OnItemSelectedListener onItemSelectedListener;

    public SelectSpinner(Context context) {
        super(context);
        registerEvents();
    }

    public SelectSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
        registerEvents();
    }

    public SelectSpinner(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        registerEvents();
    }

    /**
     * Register default events
     * */
    private void registerEvents() {
        // overrides the default event for android OnItemSelectedListener
        super.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                if (onItemSelectedListener != null) {
                    // call onItemSelected default event in android
                    onItemSelectedListener.onItemSelected(parent, view,
                            position, id);
                }
                // checks if the spinner selection is opened by the user and has event listener
                if (isUserOpen && onSelectSpinnerListeners != null) {
                    for (OnSelectSpinnerListener listener : onSelectSpinnerListeners) {
                        listener.onItemSelectByUser(parent, view, position, id);
                    }
                    isUserOpen = false;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                if (onItemSelectedListener != null) {
                    // call onNothingSelected default event in android
                    onItemSelectedListener.onNothingSelected(parent);
                }
            }
        });
    }

    public void addOnSelectSpinnerListener(
            OnSelectSpinnerListener onSelectSpinnerListener) {
        if (this.onSelectSpinnerListeners == null) {
            this.onSelectSpinnerListeners = new ArrayList<SelectSpinner.OnSelectSpinnerListener>();
        }
        this.onSelectSpinnerListeners.add(onSelectSpinnerListener);
    }

    @Override
    public void setOnItemSelectedListener(OnItemSelectedListener listener) {
        this.onItemSelectedListener = listener;
    }

    @Override
    public boolean performClick() {
        // isUserOpen variable set to true, to indicate that there was user interaction
        isUserOpen = true;
        if (this.onSelectSpinnerListeners != null) {
            for (OnSelectSpinnerListener listener : this.onSelectSpinnerListeners) {
                // call onOpenSelectMenu event
                listener.onOpenSelectMenu();
            }
        }
        return super.performClick();
    }
}
于 2014-07-10T14:44:22.897 回答
0
spinner.setOnItemSelectedListener(this)
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    //here arg2 will return the number of option which user select

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            switch (arg2) {
            case 0:

                // do something for london
                Toast.makeText(getApplicationContext(), "London",Toast.LENGTH_SHORT).show();
                break;
            case 1:
                // do something for Paris
                Toast.makeText(getApplicationContext(), "Paris",Toast.LENGTH_SHORT).show();
                break;
            case 2:
                // do something for Stockholm
                Toast.makeText(getApplicationContext(), "Stockholm",Toast.LENGTH_SHORT).show();
                break;

            default:
                break;
            }

        }
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub enter code here
}
于 2012-10-30T08:04:06.737 回答