2013-08-02 110 views
1

將切換按鈕設置爲選中狀態後,每次點擊它時,其處於相同狀態。自定義ToggleButton setChecked(..)問題

定製選擇器:

<?xml version="1.0" encoding="utf-8"?> 

<item android:drawable="@drawable/list_view_icon" android:state_checked="true" 
    android:state_pressed="true" android:state_enabled="true"/> 
<item android:drawable="@drawable/list_view_icon" android:state_checked="true" 
    android:state_focused="false" android:state_enabled="true"/> 

<item android:drawable="@drawable/map_view_icon" android:state_checked="false" 
    android:state_pressed="true" android:state_enabled="true"/> 
<item android:drawable="@drawable/map_view_icon" android:state_checked="false" 
    android:state_focused="false" android:state_enabled="true"/>  

切換按鈕:

<ToggleButton 
     android:id="@+id/toggle_button_map_or_list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:button="@drawable/custom_selector" 
     android:textOn="" 
     android:textOff="" 
     android:background="@drawable/map_view_icon"    
     /> 

java代碼:

在按鈕的onClick監聽器,isChecked永遠是假的

boolean isChecked = ((ToggleButton) view).isChecked();  

Log.i(TAG, "isChecked : "+isChecked); 
// **its always false and image also not changing.** 

請建議什麼是錯在這裏。我看過this回答,但沒用。

編輯:我做了一個示例項目與相同的情況。它的工作正常,但在我的項目片段中,其行爲不同。

+0

你可能有很好的理由,但你爲什麼不使用切換按鈕?我可以傳遞一些使用這些視圖的代碼。 – iaindownie

回答

1

這是給你的:

<?xml version="1.0" encoding="utf-8"?> 

<item android:drawable="@drawable/list_view_icon" 
     android:state_checked="true" /> 

<item android:drawable="@drawable/map_view_icon" 
     android:state_checked="false" /> 

你已經投入了很多的條件,不同的狀態,這就是造成問題的原因(見here瞭解更多信息)。

按照你編輯:

<ToggleButton 
     android:id="@+id/toggle_button_map_or_list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:button="@drawable/custom_selector" 
     android:textOn="" 
     android:textOff="" 
     android:background="@drawable/selector_name"    
     /> 

這將修復它爲您肯定!您直接使用地圖圖標,絕不會通過選擇器。

+0

我之前試過。即使對於這些簡單的狀態,一旦我設置通過Java代碼檢查,後續點擊按鈕既不改變圖像也不是狀態是假的。 – Braj

+0

好的,那麼問題是你如何使用選擇器?編輯問題 – g00dy

+0

。請看看它。 – Braj

-1

選擇器有一個命令(見Drawable States)。

刪除android:button屬性。 設置選擇爲背景,以您的ToggleButton

<ToggleButton 
    android:id="@+id/toggle_button_map_or_list" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textOn="" 
    android:textOff="" 
    android:background="@drawable/custom_selector"/> 

而且custom_selector.xml樣子:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- Checked and pressed --> 
    <item 
     android:state_checked="true" 
     android:state_pressed="true" 
     android:drawable="@drawable/list_view_icon" /> 
    <!-- Pressed --> 
    <item 
     android:state_pressed="true" 
     android:drawable="@drawable/map_view_icon" /> 
    <!-- Checked --> 
    <item 
     android:state_checked="true" 
     android:drawable="@drawable/list_view_icon" /> 
    <!-- Default (not checked) --> 
    <item 
     android:drawable="@drawable/map_view_icon"/> 
</selector> 
+0

結果是一樣的:( – Braj

+0

只需重命名您的selector.xml並將其設置爲背景。不要爲圖標和選擇器使用相同的名稱! – Nipper