简体   繁体   English

SharedPreferences 不适用于 setTextSize

[英]SharedPreferences doesnt work with setTextSize

Hello i want to implement different text sizes for my app i tried with sharedpreferences here's my code您好,我想为我的应用程序实现不同的文本大小,我尝试使用共享首选项,这是我的代码

Declaring SharedPreferences声明 SharedPreferences

final  SharedPreferences savedFields;

    savedFields = this.getSharedPreferences("stoixeiaios", 0);
    stoixeia.setText(savedFields.getString("stoixeiadieth", ""));
    stoixeia2.setText(savedFields.getString("stoixeiaonoma", ""));

    logos1.setTextSize(savedFields.getFloat("megethos1",12));
    logos2.setTextSize(savedFields.getFloat("megethos2",12));
    logos3.setTextSize(savedFields.getFloat("megethos3",12));
    logos4.setTextSize(savedFields.getFloat("megethos4",12));
    logos5.setTextSize(savedFields.getFloat("megethos5",12));
    logos6.setTextSize(savedFields.getFloat("megethos6",12));

The button which changes the text size and saves the value to sharedpreferences更改文本大小并将值保存到 sharedpreferences 的按钮

**    button4.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor preferencesEditor = savedFields.edit();
                logos1.setTextSize(TypedValue.COMPLEX_UNIT_PX,24));
                logos2.setTextSize(TypedValue.COMPLEX_UNIT_PX,24);
                logos3.setTextSize(TypedValue.COMPLEX_UNIT_PX,24);
                logos4.setTextSize(TypedValue.COMPLEX_UNIT_PX,24);
                logos5.setTextSize(TypedValue.COMPLEX_UNIT_PX,24);
                logos6.setTextSize(TypedValue.COMPLEX_UNIT_PX,24);


                preferencesEditor.putFloat("megethos1", logos1.getTextSize());
                preferencesEditor.putFloat("megethos2", logos2.getTextSize());
                preferencesEditor.putFloat("megethos3", logos3.getTextSize());
                preferencesEditor.putFloat("megethos4", logos4.getTextSize());
                preferencesEditor.putFloat("megethos5", logos5.getTextSize());
                preferencesEditor.putFloat("megethos6", logos6.getTextSize());

                preferencesEditor.commit();
            }
        });**

I don't know what i am doing wrong我不知道我做错了什么

Thank you for your time感谢您的时间

Edit : I forgot to post the problem编辑:我忘了发布问题

The problem is that the saved values after the app terminates are ALWAYS wrong for example when i click the button to change the text size , the text size changes then i terminate the app reopen it and the text size is bigger than it's supposed to be问题是应用程序终止后保存的值总是错误的,例如当我单击按钮更改文本大小时,文本大小更改然后我终止应用程序重新打开它并且文本大小大于它应该是

The problem is that you are setting the text size in pixels.问题是您正在以像素为单位设置文本大小。 What you need to do is to convert it into density pixels.您需要做的是将其转换为密度像素。

To do this you can use this method为此,您可以使用此方法

public int pxToDp(float px) {
    return (int) (px / Resources.getSystem().getDisplayMetrics().density);
}

So when you set the text size you should call因此,当您设置文本大小时,您应该调用

float textSize = savedFields.getFloat("megethos1",12);
logos1.setTextSize(pxToDp(textSize));

for every TextView.对于每个 TextView。

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

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