如何更改按钮文本的颜色在使用十六进制值单击按钮时?我在 MainActivity 上使用下面的代码,但无法正常工作
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// button.setTextColor(Color.GRAY);
button.setTextColor(Color.(#808080));
}
});
答案 0 :(得分:2)
您应使用Color.parseColor
来获取十六进制颜色字符串的int
值,如下所示:
button.setTextColor(Color.parseColor("#808080"))
答案 1 :(得分:1)
button.setTextColor(Color.parseColor("#ff0000"));
答案 2 :(得分:1)
尝试一下:
button.setTextColor(Color.parseColor("#ff0000"));
OR
button.setTextColor(0xff0000); //SET CUSTOM COLOR
OR
button.setTextColor(getApplication().getResources().getColor(R.color.red)); //TAKE DEFAULT COLOR
OR
<Button android:id="@+id/btn_my_button"
android:text="YOUR_TEXT"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#ff0000" /> <-- SET TEXT COLOR HERE -->
希望这可能现在对您有帮助
答案 3 :(得分:0)
您可以使用这种方式
button.setTextColor(Color.parseColor("#ff0000"));
答案 4 :(得分:0)
在res / values / colors.xml文件中定义颜色,并按如下方式使用它:
button.setTextColor(getColor(R.color.your_color_name));
答案 5 :(得分:0)
可以有两种方式
在res / values / colors.xml中定义您的颜色
在您的Activity.java上使用它
button.setTextColor(getColor(R.color.defined_color_name));
在activity_name.xml上使用
@IBAction func addCard(_ sender : UIBarButtonItem)
{
self.performSegue(withIdentifier:"addEditSegue", sender: sender)
}