【问题标题】:textview background color is not changing on click in popupwindow在弹出窗口中单击时,textview背景颜色没有改变
【发布时间】:2012-07-12 14:42:15
【问题描述】:

我正在使用带有文本视图的 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> 

在我的弹出窗口中,我所做的就是:

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);

 }});

弹出窗口的布局文件:

<?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>

如果我不关闭弹出窗口但如果我关闭点击时弹出窗口 textview 背景不会改变,那么它的行为非常奇怪。

我做错了什么?任何帮助将不胜感激。

【问题讨论】:

    标签: android


    【解决方案1】:

    你会像 Checkbox 一样使用你的 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();
        }
    });
    

    【讨论】:

      【解决方案2】:

      我相信如果你使用上面的代码,你会没事的:

      <?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>
      

      您不能在一个项目中定义两种不同的状态。

      希望对您有所帮助。

      【讨论】:

      • 感谢您的建议,但没有帮助,点击时 textview 背景仍然没有改变。
      • @kay 你能发布你的布局 xml 文件吗?我相信这段代码和回答你问题的其他人的代码是正确的,所以问题可能出在其他地方。
      • @kay 嗯,我也没有在您的布局 xml 上看到任何问题。由于您说当您单击关闭PopUpWindow 的按钮时会发生这种奇怪的行为,所以问题可能是PopUpWindow 阻止了按钮的UI 更新。如果您真的想更改按钮的背景,请在onClick 事件期间使用v.setBackgroundResource(R.drawable.activated) 动态设置activated 可绘制对象。也许这会触发按钮的更新,但我不确定。希望对您有所帮助。
      【解决方案3】:

      //当android:state_focused="true"也为真时,你需要删除android:state_pressed="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"

      【讨论】:

      • 那也没有起作用 padma。我点击后发生的事情是这个文本视图背景变得透明并且弹出窗口消失了。
      • 将它用于 TextView 也作为 'android:clickable="true"' ,反之亦然。
      【解决方案4】:

      检查您是否有命名冲突。如果您的任何更改都没有出现,那么由于某种命名问题与导入的库冲突而导致它无法正常工作的可能性可能是您的主要问题。

      【讨论】:

        猜你喜欢
        • 2014-06-12
        • 2011-06-05
        • 2013-09-27
        • 2013-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多