admin管理员组文章数量:1345476
In my old java app I had a dropdown from the action bar which presented a choice of adjusting the font size and the font type. This used the PreferenceFragment which has long been deprecated. I am looking for a replacement in kotlin. All I can find in the docs are preferences which is a subset of the Room database. I want to provide an xml of variables and use a preference manager to remember the choice. The journey is: user clicks on actionBar to get a dropdown with two items, font size and font name
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android=";>
<ListPreference
android:entries="@array/font_size"
android:entryValues="@array/fvalues"
android:key="fong"
android:summary="Click here to choose a font size"
android:title="Select Font Size" />
<ListPreference
android:entries="@array/font_name"
android:entryValues="@array/font_ttf"
android:key="fonz"
android:summary="Click here to change font"
android:title="Select Font" />
</PreferenceScreen>
Depending on your selection you get a choice of resources
<resources>
<string-array name="font_size">
<item>Huge</item>
<item>Large</item>
<item>Normal</item>
<item>Small</item>
<item>Tiny</item>
</string-array>
<string-array name="fvalues">
<item>28</item>
<item>24</item>
<item>20</item>
<item>16</item>
<item>12</item>
</string-array>
<string-array name="font_name">
<item>Note</item>
<item>Flower</item>
</string-array>
<string-array name="font_ttf">
<item>Note_this.ttf</item>
<item>IndieFlower.ttf</item>
</string-array>
</resources
When the user has selected a font name and font size these are then used across the app.
This is the actionBar code
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.action_settings) {
Intent p = new Intent("app.sticky_notes.PREFS");
startActivity(p);
}
return false;
}
Which calls this class, which presented the user with a second menu. It then saves the choosen preferences.
public class Prefs extends PreferenceActivity
{
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new Pref_frag())mit();
}
public static class Pref_frag extends PreferenceFragment
{
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
}
I then call the Preference manager to get the stored choices
public int get_font_size() {
SharedPreferences get_prefs = PreferenceManager.getDefaultSharedPreferences(this);
String font_string = get_prefs.getString("fong", "24");
font_size = Integer.parseInt(font_string);
return font_size;
}
public String get_font_name() {
SharedPreferences get_prefs = PreferenceManager.getDefaultSharedPreferences(this);
font_name = get_prefs.getString("fonz", "Note_this.ttf");
return font_name;
}
In kotlin, as far as I can tell, none of this is possible. All sharedPreferences point back to preferences which is a version of Room database which is used to store user input. I want the dropdown->settings menu->settings value menu so the user can click a radio button.
I don't want user input other than a choice of font size and type.
I must be missing something, can anybody point me in the right direction of a working example?
In my old java app I had a dropdown from the action bar which presented a choice of adjusting the font size and the font type. This used the PreferenceFragment which has long been deprecated. I am looking for a replacement in kotlin. All I can find in the docs are preferences which is a subset of the Room database. I want to provide an xml of variables and use a preference manager to remember the choice. The journey is: user clicks on actionBar to get a dropdown with two items, font size and font name
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android/apk/res/android">
<ListPreference
android:entries="@array/font_size"
android:entryValues="@array/fvalues"
android:key="fong"
android:summary="Click here to choose a font size"
android:title="Select Font Size" />
<ListPreference
android:entries="@array/font_name"
android:entryValues="@array/font_ttf"
android:key="fonz"
android:summary="Click here to change font"
android:title="Select Font" />
</PreferenceScreen>
Depending on your selection you get a choice of resources
<resources>
<string-array name="font_size">
<item>Huge</item>
<item>Large</item>
<item>Normal</item>
<item>Small</item>
<item>Tiny</item>
</string-array>
<string-array name="fvalues">
<item>28</item>
<item>24</item>
<item>20</item>
<item>16</item>
<item>12</item>
</string-array>
<string-array name="font_name">
<item>Note</item>
<item>Flower</item>
</string-array>
<string-array name="font_ttf">
<item>Note_this.ttf</item>
<item>IndieFlower.ttf</item>
</string-array>
</resources
When the user has selected a font name and font size these are then used across the app.
This is the actionBar code
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.action_settings) {
Intent p = new Intent("app.sticky_notes.PREFS");
startActivity(p);
}
return false;
}
Which calls this class, which presented the user with a second menu. It then saves the choosen preferences.
public class Prefs extends PreferenceActivity
{
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new Pref_frag())mit();
}
public static class Pref_frag extends PreferenceFragment
{
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
}
I then call the Preference manager to get the stored choices
public int get_font_size() {
SharedPreferences get_prefs = PreferenceManager.getDefaultSharedPreferences(this);
String font_string = get_prefs.getString("fong", "24");
font_size = Integer.parseInt(font_string);
return font_size;
}
public String get_font_name() {
SharedPreferences get_prefs = PreferenceManager.getDefaultSharedPreferences(this);
font_name = get_prefs.getString("fonz", "Note_this.ttf");
return font_name;
}
In kotlin, as far as I can tell, none of this is possible. All sharedPreferences point back to preferences which is a version of Room database which is used to store user input. I want the dropdown->settings menu->settings value menu so the user can click a radio button.
I don't want user input other than a choice of font size and type.
I must be missing something, can anybody point me in the right direction of a working example?
Share Improve this question asked 17 hours ago MunterManMunterMan 752 silver badges10 bronze badges1 Answer
Reset to default 1The package android.preference
is deprecated in favor of androidx.preference
which you can get by including this package in your gradle file
implementation "androidx.preference:preference-ktx:1.2.0"
Where you can then use all the same stuff you used before, you just might have to update some classes.
You can find an example here
本文标签: androidkotlin sharedPreferencesStack Overflow
版权声明:本文标题:android - kotlin sharedPreferences - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743759527a2534118.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论