简体   繁体   English

根据当前背景更改按钮的背景

[英]Changing background of button depending on current background

I'm trying to change the background of a button when it's clicked but also be able to change it back when it's clicked again. 我试图在单击按钮时更改其背景,但也能够在再次单击该按钮时将其更改回。 What I'm trying to do is get the buttons current background and if it is (xxxxx) then change it to (yyyyy). 我想做的是获取按钮的当前背景,如果是(xxxxx),则将其更改为(yyyyy)。 I can't seem to find how to get the button's current background and compare it to one of my drawable resources. 我似乎找不到如何获取按钮的当前背景并将其与我的可绘制资源之一进行比较的方法。
Any help would be much appreciated. 任何帮助将非常感激。

Some pseudo code of what I'm trying to do: 我正在尝试做的一些伪代码:

if (button1.getBackground() == R.drawable.someDrawable) {
   button1.setBackgroundResource(R.drawable.someOtherDrawable);
}

我认为这是正确的方法: 链接

I simple solution will be to have two drawables for background. 我的简单解决方案是将两个可绘制对象用作背景。 One for non_pressed_selector and pressed_selector and just keep track if the button is pressed or not. 一个用于non_pressed_selector和Pressed_selector,只要跟踪是否按下按钮就可以了。

private boolean isPressed = false;
public void onClick() {
    if (isPressed) {
        // set not_pressed_selector
    } else {
        // set pressed_selector
    }
    isPressed != isPressed;
}

What I did was: 我所做的是:

Declare the button in it's initial state: 在初始状态下声明按钮:

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

Then I manage the events from the code controling the actual state of the button with a tag: 然后,我通过代码管理带有标签的按钮的实际状态的事件:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drawables_buttons);  //This is the layout where it's the button

    threeStateButton = (Button)findViewById(R.id.three_States_Button);  //This is the button
    threeStateButton.setOnTouchListener(new CustomTouchListener());    
    threeStateButton.setTag("StateA");    //Set the control tag
}

private class CustomTouchListener implements View.OnTouchListener
{

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent)
    {
        switch (motionEvent.getAction())
        {
            case MotionEvent.ACTION_UP:  //When you lift your finger
                if (threeStateButton.getTag().equals("StateA"))
                {
                    threeStateButton.setBackgroundResource(R.drawable.StateB);
                    Toast.makeText(view.getContext(), "This gonna change my state from StateA to StateB",Toast.LENGTH_SHORT).show();
                    threeStateButton.setTag("StateB");
                }
                else  //If when you lift your finger it was already on stateB
                {
                    threeStateButton.setBackgroundResource(R.drawable.red_button);
                    Toast.makeText(view.getContext(), "This gonna change my state from StateB to StateA",Toast.LENGTH_SHORT).show();
                    threeStateButton.setTag("StateA");
                }
                break;
            //In case you want that you button shows a different state when your finger is pressing it.
            case MotionEvent.ACTION_DOWN:
                threeStateButton.setBackgroundResource(R.drawable.StateButtonPressed);
                break;
        }

        return false;
    }
}

I don't know if this is the best way to do it but it works, and yes, I'd like to know wich is the optimal way. 我不知道这是否是最好的方法,但它确实有效,是的,我想知道这是最佳方法。

It seems that you need a ToggleButton instead of simple Button . 似乎您需要一个ToggleButton而不是简单的Button You could use isChecked() method to determine the state of your button in OnClickListener and set your background there. 您可以使用isChecked()方法确定OnClickListener按钮的状态,并在OnClickListener设置背景。 Or you can define selector in xml as pedr0 said. 或者您可以在xml中定义选择器,如pedr0所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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