2012-07-12 67 views
5

我使用PopUpwindow與textviews中。問題是當我點擊任何textvies時,背景顏色並沒有改變,雖然它在textview被聚焦但不是點擊時改變。textview背景顏色不改變點擊popup windowow

點擊我正在駁回popupwindow,如果我不關閉該popupwindow然後根據選擇背景顏色變化後:

這是我的TextView背景選擇:

<item android:state_focused="true" android:drawable="@drawable/focused" />  
<item android:state_pressed="true" android:drawable="@drawable/pressed" /> 
<item android:drawable="@drawable/priornone" /> <!-- default --> </selector> 

在我popupwindow所有我做的是這樣的:

TextView manage_list = (TextView)popupView.findViewById(R.id.manage_lists); 
manage_list.setOnClickListener(new View.OnClickListener(){ 

public void onClick(View v) 
{ 

    Intent myIntent = new Intent(v.getContext(),ManageList.class); 
     popupWindow.dismiss(); 
    startActivity(myIntent); 

}}); 

爲popupwindow佈局文件

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
    android:background="@drawable/pop_menu_bg" 
android:orientation="vertical" 
    > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/manage_lists" 
    android:text="Manage lists" 
    android:background="@drawable/my_drawable" 
> 
</TextView> 


</LinearLayout> 

它非常奇怪的行爲一切運作良好,如果我不關閉popupwindow,但如果我忽略popupwindow單擊textview背景不會更改。

我在做什麼錯了?任何幫助將不勝感激。

回答

0

我相信,如果你使用上面的代碼,你會好起來:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_focused="true" android:drawable="@drawable/focused" /> 
<item android:state_pressed="true" android:drawable="@drawable/activated" /> 
<item android:drawable="@drawable/priornone" /> 
</selector> 

不能在項目定義兩個不同的狀態。

希望有所幫助。

+0

感謝您的建議,但它沒有幫助,仍textview背景不會改變點擊。 – kay 2012-07-12 17:07:24

+0

@kay你可以發佈你的佈局xml文件嗎?我相信這個代碼和其他回答您問題的代碼是正確的,所以問題可能在其他地方。 – Angelo 2012-07-12 17:14:43

+0

我發佈了佈局文件。 – kay 2012-07-12 17:37:32

0

//您需要刪除android:state_pressed="true"android:state_focused="true"也是如此。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
      android:drawable="@drawable/button_pressed" /> <!-- pressed --> 
    <item android:state_focused="true" 
      android:drawable="@drawable/button_focused" /> <!-- focused --> 
    <item android:state_hovered="true" 
      android:drawable="@drawable/button_focused" /> <!-- hovered --> 
    <item android:drawable="@drawable/button_normal" /> <!-- default --> 
</selector> 

refer here:

編輯:你需要給你的LinearLayout屬性作爲android:clickable="false"

+0

嘗試了您的建議,但沒有幫助,謝謝。 – kay 2012-07-12 17:08:27

+0

@kay我有你的問題。看到我編輯的答案。 – 2012-07-13 07:00:12

+0

,也沒有工作padma。點擊後發生的事情是這個textview背景變得透明,並且popupwindow解散。 – kay 2012-07-13 11:43:12

0

您將使用自己的TextView像複選框,不是嗎?

使用布爾標誌來試試這個。

private boolean clicked = false; 

// ... 

mytextView.setOnClickListener(new OnClickListener(){ 
    @Override 
    public void onClick(View v){ 
     clicked = !clicked; 

     if(clicked){ 
      mytextView.setBackgroundColor(yourcolorclicked); 
     }else{ 
      mytextView.setBackgroundColor(yourcolorunclicked); 
     } 
     mytextView.invalidate(); 
    } 
}); 
+0

嘗試了您的建議,但沒有幫助,謝謝。 – kay 2012-07-12 17:08:01

0

檢查您是否存在命名衝突。如果您的更改沒有顯示出來,則由於與導入的庫衝突的某種命名問題而導致無法正常工作的可能性可能是您的主要問題。