2012-10-30 107 views
1

選擇我嘗試在特定事件附加到Android android.widget.Spinner安卓:微調對用戶

我知道Spinner.setOnItemSelectedListener存在,但沒有工作,我想, 與setOnItemSelectedListener的問題是方法調用,而微調器加載一個適配器,並按預定義項目選擇。

我想要一個界面來獲取事件,而「用戶」通過使用tuch屏幕從微調器中選擇一個項目。此外,只有在使用時選擇一個項目而不是程序選擇一個項目時纔會調用該事件。

我想要一個事件,當使用從這個微調對話框中選擇一個項目時調用。

enter image description here

回答

3

我終於發現,我工作在代碼上http://www.oschina.net/code/explore/android-4.0.1/core/java/android/widget/Spinner.java

的解決方案,通過看我發現如何防止或附加更多代碼到運行的代碼部分,而使用會顯示下拉列表,然後創建我的o 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(); 
    } 
} 
+0

這不適用於最新的android版本,onClick永遠不會被調用。 –

+0

我發佈[此](http://stackoverflow.com/a/24679564/2290538)回答適用於更新版本的Android 4.0+ –

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(新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 
} 
+1

我不能正確理解你的問題。如果這不是你的正確答案,那麼請澄清這個問題,當用戶從微調器中選擇選項時你想要做什麼。感謝名單。 –

+0

是的,但我所說的'setOnItemSelectedListener'現在不適用於我,現在基於此將在Spinner創建並加載項目時調用,我想要一個僅在調用時纔會調用的事件該項目。 –

+0

希望它能幫助你...這就是你想要的 –

2

正如this此事提到onClick()不工作開始,但近期的Android版本(我測試了它的一些版本4.0)。

所以我適應誰在android系統的新版本適用於this 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(); 
    } 
}