开发者

stateListDrawable help

So i made these two buttons.

I want to be able to click the button i made and have the button change to the whited-out image of the original button instead of on and off states you would have in a toggle button.

so far all I've figured out is something to do with stateListDrawable xml, which i placed in the 3 drawable folders. Thats the only place I've found it belongs, however, i don't know where to reference the .xml file to the main. Do you do this programmatically? If so point me in the right direction of a good tutorial. The android dev page i开发者_运维知识库s foreign to me. And i would have posted the buttons so you would have a better idea, but i can't until i get some more points. What a gimmick!

<?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/toggle_selection_pressed" />  
 <item android:drawable="@drawable/toggle_selection" />
</selector>


You reference your drawable xml files wherever the image for your button should be.

For instance, in the layout where you place the button it would be:

<Button
<!-- Other stuff -->
android:background="@drawable/xmlfile"
>


You'll need a variable to keep the state of the button. Put this in the main class declaration

    int isClicked = 0;

    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        if (isClicked == 0){
           Demo_button.setsetImageResource(R.drawable.secondimage);
           isClicked = 1;
        }
        else{
           Demo_button.setsetImageResource(R.drawable.firstimage);
           isClicked = 0;
        }
    }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜