简体   繁体   中英

how to change button background color as clicked action?

---In android application ---

after a button clicked,its background color is changed to required. as clicked again,its color will restore original color.

How to implement it?

any response,thank you!

================================================================================== Update: From network,i find a method to achieve this aim. design a drawable color in xml like this:

<drawable name="button_checked">#ffff0000</drawable>  

in activity,use below code to get Drawable object:

Resources resource = getBaseContext().getResources();
checked_drawable = resource.getDrawable(R.drawable.button_checked);

in onClick function,according to a boolean variable:

 setBackgroundDrawable(checked_mDrawable)

to set button background.

In your js file put

$('#button').toggleClass('bg');

in your css, have your regular button css style

#button { background: white; }

and then add

#button.bg { background: red; }

So when they click on the button it will turn background red, if they click on it again, it will turn back to white.

Use whatever color / url you want for backgrounds.

You just need to create a state list drawable resource like below:

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

    <item android:drawable="@drawable/btn_login_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_login_normal" android:state_pressed="false"/>

</selector>

Updated: Maybe you want the effect as RadioButton, Here is an example:

    <RadioButton
       android:id="@+id/tab_communication"
       android:layout_width="wrap_content"
       android:layout_height="40dp"
       android:layout_weight="1"
       android:background="@null"
       android:button="@null"
       android:drawableTop="@drawable/category_communication"
       android:paddingBottom="5dp"
       android:paddingTop="5dp" />

drawable/category_communication.xml:

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

    <item android:drawable="@drawable/category_communication_checked" android:state_checked="true"/>
    <item android:drawable="@drawable/category_communication_normal" android:state_checked="false"/>

 </selector>

ToggleButton is another choice.

You can do it in code dynamically(I don't know how to configure it in xml).

boolean flag=true;
Button button=(Button)findViewById(R.id.button);
oncreate()
{
    button.setBackgroundResource(R.drawable.first_time_click);
    button.setOnClickListener(new OnClickListener()
    {
    public void onClick(View v)
    {
        if (flag)
        {
            button.setBackgroundResource(R.drawable.odd_time_click);        
        }else
        {
           button.setBackgroundResource(R.drawable.even_time_click);
        }
        flag=!flag;
    }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM