更改按钮文字颜色 Android M

Change button text color Android M

好的,所以我需要在点击两个按钮后更改它们的文本颜色,我有这个:

manButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            manButton.setTextColor(getApplication().getResources().getColor(R.color.selectedGender));
            womanButton.setTextColor(getApplication().getResources().getColor(R.color.unselectedGender));
        }
    });

但是自 Android M 以来,方法 getColor() 似乎已被弃用,并且由于我的应用仅适用于 android 棒棒糖及更高版本,因此我需要解决此问题。我一直在网上搜索,我发现从 Android M 开始从 xml 中获取颜色,你必须使用:ContextCompat.getColor(context, R.color.my_color) 我试过了,但是它说它无法解析符号上下文,我也尝试将 context 更改为 this,因为它在 Activity 中,但它给了我这个错误:

 Wrong 1st argument type. Found: 'android.view.View.OnClickListener', required: 'android.content.Context' 

getColor(android.content.Context,int) in ContextCompat cannot be applied to (anonymous android.view.View.OnClickListener,int)

那么,我该怎么办? 非常感谢!

So, what should I do?

this 替换为 WhateverYourActivityNameIs.this,其中 WhateverYourActivityNameIs 是您的 activity class 名称。