2017-02-20 41 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");