2012-07-12 68 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

检查您是否存在命名冲突。如果您的更改没有显示出来,则由于与导入的库冲突的某种命名问题而导致无法正常工作的可能性可能是您的主要问题。