我想通过selector设置 按钮被按下释放(就是手指点一下放开)后的图标更改, 不是那种按下更改,释放又变回原来的那种,如何通过xml设置?  JAVA代码设置我会。

解决方案 »

  1.   


    <?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
         <item android:state_pressed="false" android:drawable="@drawable/android_btn" />
         <item android:state_focused="true" android:drawable="@drawable/android_btn" />
         <item android:state_pressed="true" android:drawable="@drawable/android_btn_pressed" /> 
    </selector> 然后在布局中
     <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@layout/image_btn_press" />
      

  2.   


    我在布局中用的是android:src="@layout/image_btn_press" />  ,然而效果只是按下改变,松开后又变成原来的图
      

  3.   

    需要修改点东西,当然仅仅是按下松开没关系,但扩展的话需要修改修改什么东西?
    我在布局中
     <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:src="@layout/image_btn_press" />都没用
      

  4.   

    添加 android:state_selected="true"
      

  5.   

    需要修改点东西,当然仅仅是按下松开没关系,但扩展的话需要修改修改什么东西?
    我在布局中
     <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:src="@layout/image_btn_press" />都没用
    sorry,引用别人的没仔细看,需要xml中的改下顺序,如下:
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/selected" android:state_pressed="true" />
        <item android:drawable="@drawable/selected" android:state_focused="true" />
        <item android:drawable="@drawable/unselected"/>
    </selector>
      

  6.   

    简单方法就是点击设置为image1,再点击设置为image2;
    如果用selector,ImageButton不行。可以考虑用ToggleButton,background设置为selector,然后开关的text为空
    android:textOff=""
    android:textOn=""
    selector<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="选中时图片" android:state_checked="true"/>
        <item android:drawable="未选中时图片"/>
    </selector>
      

  7.   

    我试着在2个控件里面加入android:focusableInTouchMode="true"
    然后在selector里面
    <?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/dataw30" />    <item android:state_focused="true" android:drawable="@drawable/dataw30" />    <item android:drawable="@drawable/datag30"/>
    </selector>效果是打开程序,其中一个控件(控件1)已经Focused,图标变换了,然后我点击另外一个控件(控件2),控件1 失去焦点,变成原来图片,控件2获得焦点,变成设置的图片。 那么问题又来了,为什么程序初始化的时候,控件1我都没点击就已经获得了焦点呢? 我是个新手,学了一周不到。各位老师们见谅!谢谢!还有个问题,就是上面的设置, 我的2个控件(控件是Imagebutton)都是通过android:src 设置Seclector,其实我的初中就是给ImageButton换一个Icon,类似JAVA swing里面给JButton换一个默认图标,和一个点击后点亮的图标。 如果通过android:backgroud设置selector,icon图标会铺满整个背景,那就被拉长了,不是我要的效果。    ImageButton 的设置如下:
    <ImageButton
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/riptesting"
                android:src="@drawable/infobutton" />backgroud 设置了ripple效果如下
    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="#EEE9E9">
        <item>
            <shape xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="rectangle">
                <solid android:color="@color/colorGray"/>
                <corners android:radius="10px"/>
            </shape>
        </item>
    </ripple>获得焦点后还有个问题就是不单单图标变亮了,Imagebutton背景也变成了@color/colorGray
      

  8.   


        <CheckBox
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:drawableLeft="@drawable/state_checked_eee_000"
            android:button="@null"
            android:checked="true"
            android:textSize="0sp"
            />
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@color/w_c_eee"
              android:state_checked="true"/>
        <item android:drawable="@color/w_c_000"
              android:state_checked="false"/>
    </selector>拿走不谢,drawableLeft还是background自己看着办
      

  9.   

    纠正一下,如果仅仅是color选择器的话应该放在color目录,这是不规范的写法
      

  10.   

    <?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
         <android:drawable="@drawable/android_btn" />
         <item android:state_focused="true" android:drawable="@drawable/android_btn_pressed" />
         <item android:state_pressed="true" android:drawable="@drawable/android_btn_pressed" /> 
    </selector> 然后在布局中
     <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@layout/image_btn_press" />需要修改点东西,当然仅仅是按下松开没关系,但扩展的话需要修改