简体   繁体   English

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

[英]Android Studio, Shared Preferences Set Text Color

At the moment I am trying to run this lines of code: 目前,我正在尝试运行以下代码行:

    @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();
}
  • I am trying to set and store the colors that user choices to allow them to type in that color, but whenever I try to run the app or do anything within the app, it crashes because of the setTextColor function and the sharePreferences within the string for it. 我试图设置和存储用户选择的颜色以允许他们键入该颜色,但是每当我尝试运行该应用程序或在该应用程序中执行任何操作时,由于setTextColor函数和字符串中的sharePreferences导致崩溃它。

here is logcat image 这是logcat图像

在此处输入图片说明

As per you log cat image you have IllegalArgumentException exception 按照您记录的猫图像,您有IllegalArgumentException异常

You have color string which is not in correct format see this Color.parseColor 您的颜色字​​符串格式不正确,请参阅此Color.parseColor

Parse the color string, and return the corresponding color-int. 解析颜色字符串,并返回相应的color-int。 If the string cannot be parsed, throws an IllegalArgumentException exception. 如果无法解析该字符串,则抛出IllegalArgumentException异常。 Supported formats are: #RRGGBB #AARRGGBB or one of the following names: 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray', 'grey', 'lightgrey', 'darkgrey', 'aqua', 'fuchsia', 'lime', 'maroon', 'navy', 'olive', 'purple', 'silver', 'teal'. 支持的格式为:#RRGGBB #AARRGGBB或以下名称之一:'红色','蓝色','绿色','黑色','白色','灰色','青色','品红色','黄色' ,“浅灰色”,“ darkgray”,“灰色”,“ lightgrey”,“ darkgrey”,“ aqua”,“ fuchsia”,“ lime”,“ maroon”,“ navy”,“ olive”,“ purple”,“银”,“蓝绿色”。

Change 更改

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

to

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

This would be proper. 这是正确的。 Else use Color.NAME_OF_COLOR , many are covered here. 否则使用Color.NAME_OF_COLOR ,此处涵盖了许多内容。

In Android You need to provide either 6 or 8 HexaColor code. 在Android中,您需要提供6或8个HexaColor代码。 As it supported RGB or ARGB. 由于它支持RGB或ARGB。 every two character define value of once color while making final color. 每两个字符定义一次颜色的值,同时确定最终颜色。 Like:- #112233 像:-#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)

Same if you have alpha than define first two digit with alpha value:- Like with 50% opacity same above color HexaValue will be:- #80112233 如果您具有Alpha,则用Alpha值定义前两位数字相同:-像不透明度为50%一样,上面的颜色HexaValue将是:-#80112233

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

So correct solution for your problem will be:- 因此,针对您的问题的正确解决方案将是:-

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

instead of 代替

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM