setTextColor
定义为
setTextColor(int color)
正确的使用方法为
btn.setTextColor(context.getResources().getColor(R.color.red));
context的传递
context,SDK中对其说明如下:
Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc
在很多时候,在执行操作时,需要传入context参数。比如
Toast.makeText(context, "吐司", Toast.LENGTH_LONG).show();
这个context实际上就是传入当前的Activity,告诉Toast是这个Activity需要一份吐司,就像点菜一样。如果没有客人(Activity)点菜,就无法显示Toast。
曾经犯过一个错误,就是在一个Thread中调用函数,传入Activity,出现函数不执行。这是因为在Thread中已经是另外一个线程,多线程之间只能利用handler进行通信,现在的Activity(context)在另一个线程不存在,所以传过去的context为空。因此出现错误。