搜尋此網誌

2014年10月20日 星期一

【Android】Android ImageButton with a selected state?


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


And then in java:
//assign the image in code (or you can do this in your layout xml)
imageButton.setImageDrawable(getBaseContext().getResources().getDrawable(R.drawable....));

//set the click listener
imageButton.setOnClickListener(new OnClickListener() {

    public void onClick(View button) {
        //Set the button's appearance
        button.setSelected(!button.isSelected());

        if (button.isSelected()) {
            //Handle selected state change
        } else {
            //Handle de-select state change
        }

    }

}); 
from http://stackoverflow.com/questions/2604599/android-imagebutton-with-a-selected-state

沒有留言: