5

テキストビューを含む PopUpwindow を使用しています。問題は、テキストビューのいずれかをクリックしても、テキストビューがフォーカスされているがクリックされていないときに背景色が変化しているにもかかわらず、背景色が変化しないことです。

クリックした後、 popupwindow を閉じます。popupwindow を閉じないと、セレクターに従って背景色が変わります。

これは私のテキストビューの背景セレクターです:

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

その非常に奇妙な動作は、ポップアップウィンドウを閉じない場合はすべてうまく機能しますが、テキストビューの背景をクリックしてポップアップウィンドウを閉じても変更されません。

私は何を間違っていますか?どんな助けでも大歓迎です。

4

4 に答える 4

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>

1 つのアイテムに 2 つの異なる状態を定義することはできません。

それが役立つことを願っています。

于 2012-07-12T14:49:05.033 に答える
0

android:state_pressed="true"// whenandroid:state_focused="true"も 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>

ここを参照してください:

編集: Linearlayout 属性を次のように指定する必要があります android:clickable="false"

于 2012-07-12T14:50:09.063 に答える
0

TextView を Checkbox のように使用しますね。

ブール値フラグを使用してこれを試してください。

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();
    }
});
于 2012-07-12T14:50:27.233 に答える