【问题标题】:Changing background of button depending on current background根据当前背景更改按钮的背景
【发布时间】:2011-07-16 10:04:17
【问题描述】:

我正在尝试在单击按钮时更改按钮的背景,但也可以在再次单击按钮时将其更改回来。我要做的是获取按钮的当前背景,如果是(xxxxx),则将其更改为(yyyyy)。我似乎找不到如何获取按钮的当前背景并将其与我的可绘制资源之一进行比较。
任何帮助将不胜感激。

我正在尝试做的一些伪代码:

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

【问题讨论】:

    标签: java android button background-color


    【解决方案1】:

    我认为这是正确的方法:link

    【讨论】:

    • 谢谢佩德罗,“包”是我在创建项目时声明的包吗?这是您发布的链接中所说的内容...在 XML 中:@[package:]drawable/filename
    • 这是我自己的简单选择器:schemas.android.com/apk/res/android"> 跨度>
    【解决方案2】:

    我的简单解决方案是为背景设置两个可绘制对象。一个用于 non_pressed_selector 和pressed_selector,只要跟踪按钮是否被按下。

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

    【讨论】:

      【解决方案3】:

      您似乎需要ToggleButton 而不是简单的Button。您可以使用isChecked() 方法来确定OnClickListener 中按钮的状态并在那里设置您的背景。或者你可以像pedr0所说的那样在xml中定义选择器。

      【讨论】:

        【解决方案4】:

        我所做的是:

        声明按钮处于初始状态:

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

        然后我从代码中管理事件,用标签控制按钮的实际状态:

        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;
            }
        }
        

        我不知道这是否是最好的方法,但它有效,是的,我想知道这是最佳方法。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-10-11
          • 2014-11-19
          • 1970-01-01
          • 2014-01-25
          • 2011-11-25
          • 2015-04-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多