Android:Spinner由用户选择

时间:2012-10-30 07:34:17

标签: android android-spinner

我尝试将选择事件附加到我的Android android.widget.Spinner

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

我想要一个用于获取事件的界面,而“用户”通过使用tuch屏幕从Spinner中选择一个项目。此外,只有在使用选择项目而不是程序选择项目时才调用的事件。

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

enter image description here

3 个答案:

答案 0 :(得分:3)

通过查看http://www.oschina.net/code/explore/android-4.0.1/core/java/android/widget/Spinner.java

上的代码,我终于找到了适用于我的解决方案

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

我的微调器类有一个use define interface 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();
    }
}

答案 1 :(得分:2)

this中提到的那样,onClick()无法启动,但最新版本的android(我在4.0的某些版本上测试过它)。

所以我调整了this FIG-GHD742的答案,他们在较新版本的android中工作(在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();
    }
}

答案 2 :(得分: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
}