Android Button color changing on onClick?

2019-01-24 18:05发布

I am facing a problem .

I have two Button object.

ButtonA ButtonB

Requirement:-

When ever I press ButtonA the color of button should be changed and it should remain same until I clicked on ButtonB.

After click on ButtonB same thing should be working i.e for ButtonA

if (v == btn)
{
    btn.setBackground(mActivity.getResources().getDrawable(R.drawable.button_color_chnager));
}

XML:

<item android:state_focused="true" android:state_pressed="true" 
            android:drawable="@drawable/ic_launcher" /> 

4条回答
乱世女痞
2楼-- · 2019-01-24 18:17

I'm not tested below code. but i think this will helpful to you.

private Drawable defaultDrawableA;
private Drawable defaultDrawableB;
private Button buttonA , buttonB;


buttonA = (Button) findViewById(R.id.buttonA);
buttonB= (Button) findViewById(R.id.buttonB);
    buttonA .setOnClickListener(this);
    buttonB.setOnClickListener(this);


@Override
public void onClick(View v) {
    if (v == buttonA)

if(defaultDrawableA== null)
{
    defaultDrawableA=buttonA.getDrawable();
    buttonA.setBackgroundColor(Color.BLUE);
}
else
{
    buttonA.setBackgroundDrawable(defaultDrawableA);
    defaultDrawableA=null;
}
    else if (v == buttonB)

if(defaultDrawableB == null)
{
    defaultDrawableB=buttonB.getDrawable();
    buttonB.setBackgroundColor(Color.RED);
}
else
{
    buttonB.setBackgroundDrawable(defaultDrawableB);
    defaultDrawableB=null;
}

    return;
}
查看更多
神经病院院长
3楼-- · 2019-01-24 18:23

buttoncolor.xml

<?xml version="1.0" encoding="utf-8"?> 
  <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
      <item android:state_focused="true" android:state_pressed="true" 
            android:drawable="@drawable/bgalt" /> 
      <item android:state_focused="false" android:state_pressed="true" 
            android:drawable="@drawable/bgalt" /> 
      <item android:drawable="@drawable/bgnorm" /> 
  </selector>

Now use like below:

b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
         b2.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever));
    }
});

b2 = (Button) findViewById(R.id.b2);
b2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
         b1.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever));
    }
});
查看更多
家丑人穷心不美
4楼-- · 2019-01-24 18:23

Use below code..

Boolean isOnePressed = false, isSecondPlace = false;
b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                isOnePressed = true;
                b1.setBackgroundColor(Color.BLUE);
                if (isSecondPlace) {
                    b2.setBackgroundColor(Color.WHITE);
                    isSecondPlace = false;
                }

            }
        });
        b2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                b2.setBackgroundColor(Color.BLUE);
                isSecondPlace = true;
                if (isOnePressed) {
                    b1.setBackgroundColor(Color.WHITE);
                    isOnePressed = false;
                }

            }
        });

Hope it helps...

查看更多
The star\"
5楼-- · 2019-01-24 18:31

Try this:

final Button buttonA = (Button) findViewById(R.id.ButtonA);
final Button buttonB = (Button) findViewById(R.id.ButtonB);

buttonA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
buttonB.setBackgroundColor(Color.CYAN);
}
});

buttonB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
buttonA.setBackgroundColor(Color.RED);
}
});

or

make selector.xml in drawables folder and copy code below in it:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
  <item android:state_focused="true" android:state_pressed="true" 
    android:drawable="@drawable/focused_pressed" /> 
  <item android:state_focused="false" android:state_pressed="true" 
    android:drawable="@drawable/pressed" /> 
  <item android:drawable="@drawable/normal" /> 
</selector>

copy this code for buttonA in your layout

<Button
    android:id="@+id/buttonA"
    style="@drawable/test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Button A" />
查看更多
登录 后发表回答