Android: how to use selector?

只愿长相守 提交于 2019-12-18 09:44:05

问题


I have a problem of using selector that it does not work as what I expect. I wanna click on it then it gives me reaction and I select it(By long click but I probably do it through programmatic way) then it gives me another reaction. However, it reacts nothing in result....

reaction part:

 <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:weightSum="10"
                    android:padding="5dp"
                    android:background="@drawable/border_bottom"
                    >
                    <LinearLayout 
                        android:layout_weight="9"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:background="@drawable/selector_row">
                        <TextView 
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/grey"
                            android:text="@string/tel"/>
                        <TextView 
                            android:id="@+id/telText"
                            android:layout_width="wrap_content"
                            android:textSize="18sp"
                            android:layout_height="wrap_content"
                            android:text="@string/blank"/>
                    </LinearLayout>
                    <ImageButton
                        android:layout_weight="1"
                        android:id="@+id/tel_call"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:src="@drawable/ic_action_call"
                        android:background="@drawable/border_left"/>                    
                </TableRow>

selector_row.xml:

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item 
        android:state_pressed="true"
        android:drawable="@color/semitransparent_grey"></item>
    <item 
        android:state_selected="true"
        android:drawable="@color/semitransparent_blue"></item>
    <item 
        android:drawable="@color/transparent"></item>
    </selector>

回答1:


Thank you to all brothers trying hard to answer me. I have got the answer....just simply by setting android:clickable="true" at LinearLayout. Ha, it's silly.....

I remember dude who provide relating information but I ignored... sorry to him.




回答2:


Create an XML file and place it in the drawable folder. Open it and write the following code:

<selector
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item 
        android:drawable="YOUR IMAGE OR COLOR"

         android:state_pressed="true"/>
    <item
        android:drawable="YOUR IMAGE OR COLOR"

          android:state_selected="true"/>
    <item
        android:drawable="YOUR IMAGE OR COLOR"

           android:state_focused="true"/>

</selector>

Then in your main XML file, place android:background="@drawable/selector.xml"




回答3:


What are you expecting? A reaction only when it's clicked?

Try removing removing:

android:state_selected="true" 

Edit: This should be your selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/my_drawable" android:state_selected="true"></item>
    <item android:drawable="@drawable/my_drawable" android:state_pressed="true"></item>
    <item android:drawable="@drawable/my_drawable"></item>

</selector>

What is the behaviour that you are experiencing?

Edit:

<LinearLayout 
     android:layout_weight="9"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical"
     android:background="@drawable/selector_row"
     android:duplicateParentState=true>

I don't know if it actually solves the problem



来源:https://stackoverflow.com/questions/24905047/android-how-to-use-selector

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!