Android 根据HTTP请求的响应更改开关按钮的颜色
我正在实施一个开关按钮,可以远程打开或关闭灯 只要按下开关按钮,就会向服务器发送HTTP请求,并返回响应 要求是: 当应用程序等待响应时,开关按钮变为黄色 如果响应为200 OK,则按钮变为绿色 如果响应被拒绝或超时,按钮将变为红色 我正在使用默认的开关按钮。它不允许我动态改变颜色,我一直在四处寻找,找不到任何可以用于我的应用程序的东西Android 根据HTTP请求的响应更改开关按钮的颜色,android,Android,我正在实施一个开关按钮,可以远程打开或关闭灯 只要按下开关按钮,就会向服务器发送HTTP请求,并返回响应 要求是: 当应用程序等待响应时,开关按钮变为黄色 如果响应为200 OK,则按钮变为绿色 如果响应被拒绝或超时,按钮将变为红色 我正在使用默认的开关按钮。它不允许我动态改变颜色,我一直在四处寻找,找不到任何可以用于我的应用程序的东西 我如何才能实现此多状态切换按钮?您可以像这样更改颜色。我刚刚测试过,它可以工作: // Assuming ToggleButton with id "toggl
我如何才能实现此多状态切换按钮?您可以像这样更改颜色。我刚刚测试过,它可以工作:
// Assuming ToggleButton with id "toggleButton"
final ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
toggle.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
} else {
// The toggle is disabled
toggle.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorAccent));
}
}
});
举个例子。这就是你的意思吗?当然,您可以在“colors.xml”文件中制作自己的颜色。最简单的解决方案是使用SwitchCompat。尝试查看此处或此处以及其他2个–DrawableCompat或ColorFilter