使用 settingPreference 时 SetTextSize 出错

Error on SetTextSize by using settingPreference

如何从设置首选项片段到 textView 中设置文本大小?字体大小是从 listView 中选择的,我将在图片下方提供代码。

请看我下面的图片:

我知道我做错了什么。请告诉我正确的方法。

这是我的字体大小值:

这是我的首选项:

<ListPreference
        android:title="Font Size"
        android:summary="Select desirable font size"
        android:key="FontSizeKey"
        android:entries="@array/select_font_size"
        android:entryValues="@array/select_font_size_value"
        android:defaultValue="24"/>

而且如果你们有任何website/tutorial/blogs link 给我参考,那将是很好的。

使用此方法将字符串转换为浮点数

secDuaText.setTextSize(Float.parseFloat(setFontSize));

TextView接受的值为float。还有一件事是要从 SharedPreference 获取值,请使用 KEY。

String setFontSize  = getSharedPreferences.getString("FontSizeKey");
setDuaText = setTextSize(Float.valueOf(setFontSize));

从提示中可以很清楚地看出您的方法需要一个浮点值。您正在向它传递一个字符串值。因此,要么在共享首选项中使用 putDouble 将其保存为浮点数,要么使用 getDouble 并将其保存在浮点变量中。喜欢下面

SharedPrefs.putDouble("FontSizeKey", 24.0);

double setFontSize = SharedPrefs.getDouble("FontSizeKey");

否则,只需解析字符串以获得如下所示的浮点值

secDuaText.setTextSize(Float.parseFloat(setFontSize));