2010-11-22 71 views
2

我在表單活動中遇到了spinners問題。spinners and focus

我曾期待一位微調員在用戶「觸摸」它時獲得焦點,但這似乎並沒有發生。如果我使用我的跟蹤球(在Nexus One上)在不同組件之間移動,那麼微調似乎只能獲得關注。

這很煩人,因爲我在窗體的第一個EditText視圖中使用了android:selectAllOnFocus =「true」屬性。因爲spinners從未將注意力從EditText組件中移除,所以它的內容總是被高亮顯示(這是醜陋的IMO)。

我使用

spinner.requestFocus();

嘗試,但這個(貌似)沒有任何影響。

我試過要求注重微調在AdapterView.OnItemSelectedListener但他只是導致

Window already focused, ignoring focus gain of: [email protected]

誰能解釋這個奇怪的行爲和/或周圍可能的方式。

非常感謝,

+0

我在表單中有相同的行爲,但我的表單因爲某種原因運行,每當我從微調框中選擇一些內容時,我都會收到此警告。 – JPM 2011-10-13 16:58:02

回答

1

你必須首先使用setFocusableInTouchMode()。然後你遇到一個不同的問題:你必須點擊微調器兩次來改變它(一次設置焦點,然後再次看到選項列表)。我的解決方案是創建自己的微調子類,可使從第一點觸對焦增益模擬第二:

class MySpinnerSubclass extends Spinner { 

    private final OnFocusChangeListener clickOnFocus = new OnFocusChangeListener() { 

     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 

      // We don't want focusing the spinner with the d-pad to expand it in 
      // the future, so remove this listener until the next touch event. 
      setOnFocusChangeListener(null); 
      performClick(); 
     } 
    }; 

    // Add whatever constructor(s) you need. Call 
    // setFocusableInTouchMode(true) in them. 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 

     int action = event.getAction(); 
     if (action == MotionEvent.ACTION_DOWN) { 

      // Only register the listener if the spinner does not already have 
      // focus, otherwise tapping it would leave the listener attached. 
      if (!hasFocus()) { 
       setOnFocusChangeListener(clickOnFocus); 
      } 
     } else if (action == MotionEvent.ACTION_CANCEL) { 
      setOnFocusChangeListener(null); 
     } 
     return super.onTouchEvent(event); 
    } 
} 

要給予適當的信貸,我得到了我的靈感來自Kaptkaos's answerthis question