Android Studio,共享首选项设置文本颜色

时间:2017-02-20 03:36:19

标签: android colors android-sharedpreferences

目前我正在尝试运行这行代码:

let buf = new Buffer(xls, 'binary');

return reply(buf)
    .encoding('binary')
    .type('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    .header('content-disposition', `attachment; filename=test-${new Date().toISOString()}.xlsx;`);
  • 我正在尝试设置和存储用户选择的颜色以允许他们输入该颜色,但每当我尝试运行应用程序或在应用程序中执行任何操作时,由于setTextColor函数和内部的sharePreferences,它会崩溃它的字符串。

这里是logcat图像

enter image description here

3 个答案:

答案 0 :(得分:0)

根据您的日志猫图像,您有 IllegalArgumentException异常

您的颜色字​​符串格式不正确请参阅 Color.parseColor

  

解析颜色字符串,并返回相应的color-int。如果   无法解析字符串,抛出IllegalArgumentException异常。   支持的格式为:#RRGGBB #AARRGGBB或以下之一   名称:' red',' blue',' green',' black',' white',&#39 ;灰色','青色',   ' magenta',' yellow',' lightgray',' darkgray',' grey',' lightgrey& #39 ;,   ' darkgrey',' aqua',' fuchsia',' lime',' maroon',' navy& #39;,'橄榄',   ' purple',' silver',' teal'。

更改

String s3 = sharedPreferences.getString("font_color", "#000");

String s3 = sharedPreferences.getString("font_color", "#000000");

答案 1 :(得分:0)

String s3 = sharedPreferences.getString("font_color", "#000000");

这是正确的。 否则使用Color.NAME_OF_COLOR,这里有很多内容。

答案 2 :(得分:0)

在Android中你需要提供6或8个HexaColor代码。 因为它支持RGB或ARGB。 每两个字符定义一次颜色的值,同时制作最终颜色。 喜欢: - #112233

11 define Red color with value 17 from (0 to 255 range)

22 define Green color with value 34 from (0 to 255 range)

33 define Blue color with value 51 from (0 to 255 range)

如果您使用alpha而不是使用alpha值定义前两位数字,则相同: - 与50%不透明度相同,上面的颜色HexaValue将是: - #80112233

where 80 is 128 alpha value from (0 to 255 range)

因此,您的问题的正确解决方案将是: -

String s3 = sharedPreferences.getString("font_color", "#000000");

而不是

String s3 = sharedPreferences.getString("font_color", "#000");