2017-02-20 39 views
0

在此刻,我试图运行的代码,这行:Android Studio中,共享偏好设置文本颜色

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setTitle("Tell me"); 
    setContentView(R.layout.activity_post); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true) 
    ; 
    editText = (EditText) findViewById(R.id.editText1); 
    textView = (TextView) findViewById(R.id.textView); 

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
    String s = sharedPreferences.getString("font_list", "null"); 
    Typeface face = Typeface.createFromAsset(getAssets(), "fonts/" + s); 
    editText.setTypeface(face); 

    String s2 = sharedPreferences.getString("font_size", "8"); 
    editText.setTextSize(Float.parseFloat(s2)); 

    String s3 = sharedPreferences.getString("font_color", "#000"); 
    editText.setTextColor(Color.parseColor(s3)); 



    // File directory = new File(path); 
    // directory.mkdirs(); 
} 
  • 我想设置并存储的色彩,用户的选择,让他们输入在那种颜色下,但每当我尝试运行应用程序或在应用程序中执行任何操作时,它都会因为setTextColor函数和字符串中的sharePreferences而崩溃。

这里是logcat的图像

enter image description here

+0

你可以分享你的logcat吗? – Nhan

+0

一切工作正常的设置文本的字体和大小。它由于某种原因运行设置文本颜色时崩溃。 – EzioBahin

+0

logcat?不知道那是什么,我是新的android工作室。 – EzioBahin

回答

0

按您登录猫像你抛出:IllegalArgumentException异常

你有颜色字符串这是不正确的格式见本Color.parseColor

解析颜色字符串,并返回相应的color-int。如果 字符串不能被解析,则抛出IllegalArgumentException异常。 支持的格式为:#RRGGBB #AARRGGBB或以下其中一种 名称:'红','蓝','绿','黑','白','灰色','青色' 'yellow','lightgray','darkgray','gray','lightgrey', 'darkgrey','aqua','fuchsia','lime','maroon','navy','olive' '紫色','银','蓝绿色'。

变化

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

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

也投票了,这表明答案是有帮助的 –

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

这将是适当的。 其他使用Color.NAME_OF_COLOR,这里覆盖很多。

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) 

相同的,如果你有阿尔法比定义前两个数字与阿尔法值: - 就像用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");