ll.setOnClickListener(v -> {
if (currentSelected != null) {
currentSelected.setBackgroundColor(Color.WHITE); // 恢復默認
}
v.setBackgroundColor(Color.GREEN); // 選中高亮
currentSelected = (LinearLayout) v;
});
定義 `res/drawable/selector_bg.xml`:
<selector xmlns:android="...">
<item android:state_selected="true" android:drawable="@color/green" />
<item android:drawable="@color/white" />
</selector>
佈局中 `android:background="@drawable/selector_bg"`,代碼中 `v.setSelected(true/false)` 切換狀態,無需手動設顏色。