Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

ListView 中 Button 搶佔焦點的處理方式

編輯:關於 Android 開發

ListView 中 Button 搶佔焦點的處理方式

在 ListView 的自訂項目中,如果 item 內含 Button、ImageButton 或 CheckBox 等可取得焦點的子元件,可能會導致點擊 item 沒有反應,因為焦點被子元件搶走。

解法是在 item 的根配置加入 android:descendantFocusability="blocksDescendants",並將 Button 設為 android:focusable="false"。這樣 Button 與 ListView item 就能各自處理自己的點擊事件。

descendantFocusability 有三種值:beforeDescendants、afterDescendants、blocksDescendants。最常用的是 blocksDescendants,它會讓父容器覆蓋子元件的焦點行為,避免 item 點擊失效。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved