Android:以编程方式更改按钮背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3097710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: change button background programmatically
提问by Federico klez Culloca
I have this resource file for colors
我有这个颜色的资源文件
<resources>
<color name="CLR_BLUE">#00f</color>
<color name="CLR_RED">#f00</color>
<color name="CLR_GREEN">#0f0</color>
<color name="CLR_YELLOW">#ff0</color>
<color name="CLR_BLUE_DARK">#00a</color>
<color name="CLR_RED_DARK">#a00</color>
<color name="CLR_GREEN_DARK">#0a0</color>
<color name="CLR_YELLOW_DARK">#aa0</color>
</resources>
And this method that gets called when the user clicks one of four colored Buttons
当用户单击四种颜色之一时调用此方法 Buttons
private void changeBackground(Button pressedBtn)
{
int oldColor = 0;
int newColor = 0;
if(pressedBtn == greenBtn) {
oldColor = R.color.CLR_GREEN;
newColor = R.color.CLR_GREEN_DARK;
}
else if (pressedBtn == redBtn) {
oldColor = R.color.CLR_RED;
newColor = R.color.CLR_RED_DARK;
}
else if (pressedBtn == yellowBtn) {
oldColor = R.color.CLR_YELLOW;
newColor = R.color.CLR_YELLOW_DARK;
}
else if (pressedBtn == blueBtn) {
oldColor = R.color.CLR_BLUE;
newColor = R.color.CLR_BLUE_DARK;
}
else return;
pressedBtn.setBackgroundResource(newColor);
SystemClock.sleep(500);
pressedBtn.setBackgroundResource(oldColor);
}
The problem is the color of the Button doesn't change when pressed.
I stepped with a debugger and it actually reaches the right points in the method, so that's not an issue. I think the problem is in pressedBtn.setBackgroundResource(newColor)
but I can't understand why.
问题是按钮的颜色在按下时不会改变。我使用了一个调试器,它实际上达到了方法中的正确点,所以这不是问题。我认为问题出在pressedBtn.setBackgroundResource(newColor)
但我不明白为什么。
PS: Anyway, if you have a better solution to change a button color when pressed and, after a half second, change back to original color, let me know.
PS:无论如何,如果您有更好的解决方案在按下时更改按钮颜色,并在半秒后更改回原始颜色,请告诉我。
回答by Sandy1978
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.
我们试图在 Android 中仅使用普通按钮来实现类似选项卡的功能。我没有成功使用 XML 获得正确的行为 - 一定是做错了什么。例如,只要按下按钮,我就可以将按钮颜色更改为黄色,但离开时它会恢复为原始颜色。
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);
回答by Levara
You could use an xml file like one below, to create states for your button.
您可以使用如下所示的 xml 文件为按钮创建状态。
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
有关可用属性的信息在这里。您只需将此 xml 文件复制到项目中的 drawables 文件夹中,将其命名为例如 custom_button.xml,并在您的布局中引用它
android:background="@drawable/custom_button"
android:background="@drawable/custom_button"
Here is xml file...
这是xml文件...
<?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>
回答by Cheryl Simon
setBackgroundResourceis expecting a drawable, not a color. You want setBackgroundColor.
setBackgroundResource期待可绘制,而不是颜色。你想要setBackgroundColor。
Also, your colors should have at least 6 digits, #RRGGBB
此外,您的颜色应该至少有 6 位数字,#RRGGBB
I.e., for blue: #0000FF
即,蓝色:#0000FF
I think what you have might be equivalent to #00000F, which is close to black...
我认为你拥有的可能相当于#00000F,接近黑色......
And finally, you should never sleep in the UI thread (or change UI items not in the UI thread). See Painless Threadingfor a number of different ways of using other threads. I think that postDelayedmight be what you are looking for.
最后,永远不要在 UI 线程中休眠(或更改不在 UI 线程中的 UI 项)。有关使用其他线程的多种不同方式,请参阅无痛线程。我认为postDelayed可能是你正在寻找的。