0
我开发了一个应用程序,并且在我的s7和其他设备上一切正常,但我在运行4.4 kitkat的较旧的应用程序上进行了测试,当应用程序比较按钮颜色时,4.4 ,它不起作用。Android比较ConstantState不适用于4.4 KitKat
这是一段代码,我使用的是:
for (Button btn : selectorArrayList) {
final Button button = btn;
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*
Check if button color is white or red
if white, turn red and activate the
selector (category) and if it's red
turn white and deactivate the selector
category)
*/
// I've set the background in the XML Layout as R.color.white
Drawable pd = (Drawable) button.getBackground();
if (pd.getConstantState().equals(ContextCompat.getDrawable(getContext(), R.color.white).getConstantState())) {
...
在对操作系统的较新版本非常一点上决定了美国是平等的,并在4.4(没试过其他版本)他们不是。
有没有人知道我在这里做错了什么?谢谢。
尝试使用ColorDrawable得到白色 –
它的工作比较像这样的:if((pd.getColor()0XFFFFFF)== 0XFFFFFF),你知道为什么它不以另一种方式工作的原因? – DomeWTF
您在ContextCompat.getDrawable方法中传递color res,也许旧版本无法正确处理它。 –