Android: change button background programmatically

前端 未结 3 908
醉酒成梦
醉酒成梦 2021-01-14 00:47

I have this resource file for colors


    #00f
    #f00

        
相关标签:
3条回答
  • 2021-01-14 01:17

    setBackgroundResource is expecting a drawable, not a color. You want setBackgroundColor.

    Also, your colors should have at least 6 digits, #RRGGBB

    I.e., for blue: #0000FF

    I think what you have might be equivalent to #00000F, which is close to black...

    And finally, you should never sleep in the UI thread (or change UI items not in the UI thread). See Painless Threading for a number of different ways of using other threads. I think that postDelayed might be what you are looking for.

    0 讨论(0)
  • 2021-01-14 01:19

    We were trying to implement the tab like functionality using just plain buttons in Android. I was unsuccessful to obtain the right behavior using the XML- must be doing something wrong. For instance, I could get the button color to change to yellow as long as the button was pressed, but it would go back to original color when left.

    Finally, I could achieve the desired behavior by using following code in the click event handler for each of the tab buttons. Hope this helps someone having similar issue as me.

    // Get Handle for the Tab buttons
    Button btnTab1 = (Button) findViewById(R.id.button_tab1);
    Button btnTab2 = (Button) findViewById(R.id.button_tab1);
    
    // set the colors correctly
    btnTab1.setBackgroundResource(color.lightblue);
    btnTab2.setBackgroundResource(color.darkblue);
    
    0 讨论(0)
  • 2021-01-14 01:28

    You could use an xml file like one below, to create states for your button.

    The info about attributes available is here. You simply copy this xml file to your drawables folder in your project, name it for example custom_button.xml, and reference it in your layout with

    android:background="@drawable/custom_button"

    Here is xml file...

    <?xml version="1.0" encoding="utf-8"?>
    <selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item android:state_pressed="true" >
        <shape android:shape="rectangle">
            <solid
                android:color="#00ff00" />
            <stroke
                android:width="5dp"
                android:color="#ff0000"
                android:dashWidth="3dp"
                android:dashGap="2dp" />
        </shape>
    </item>
    
    <item android:state_focused="true" >
        <shape>
            <gradient
                android:endColor="#ffffff"
                android:centerColor="#ffffff"
                android:startColor="#ffffff"
                android:angle="270" />
            <stroke
                android:width="3dp"
                android:color="#00ff00" />
            <corners
                android:radius="5dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    
    <item>        
        <shape>
            <gradient
                android:endColor="#ffffff"
                android:centerColor="#ffffff"
                android:startColor="#ffffff"
                android:angle="270" />
            <stroke
                android:width="5dp"
                android:color="#00ff00" />
            <corners
                android:radius="5dp" />
        </shape>
    </item>
    

    0 讨论(0)
提交回复
热议问题