【发布时间】:2015-10-26 00:17:23
【问题描述】:
我是 Android 新手,我被卡住了 6 个小时。
问题是我不知道首选项文件的名称,我需要从首选项文件中获取值。我正在使用 Android Studio 并创建了一个“设置活动”。除了SettingsActivity.java,我一直没有给任何文件命名。
所以我的问题是共享首选项文件的名称是什么(因为应用程序保留了这些值)。或者,如果有办法找出来。
或者我在代码中遗漏了一些明显的东西。以下是我的相关代码。
String key = "example_text";
final String PREF_FILE_NAME = "SettingsActivity";
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
String value = preferences.getString(key, " null");
编辑 1:我有一个名为 RemoteDevice.java 的活动,在此活动中我有一个用于互联网使用的异步任务子类。现在我已经通过上面提到的 PreferencesActivity 存储了 IP 地址,现在想要检索它。但是找不到。
编辑 2: 在下面的代码中,我试图从编辑文本中获取价值。
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- NOTE: EditTextPreference accepts EditText attributes. -->
<!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->
<EditTextPreference
android:key="example_text"
android:title="@string/pref_host_ip_address"
android:defaultValue="@string/pref_default_host_address"
android:selectAllOnFocus="true"
android:inputType="numberDecimal"
android:digits="123456789."
android:capitalize="words"
android:singleLine="true"
android:maxLines="1" />
<!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to
dismiss it. -->
<!-- NOTE: ListPreference's summary should be set to its value by the activity code. -->
<ListPreference
android:key="example_list"
android:title="@string/pref_title_add_friends_to_messages"
android:defaultValue="-1"
android:entries="@array/pref_example_list_titles"
android:entryValues="@array/pref_example_list_values"
android:negativeButtonText="@null"
android:positiveButtonText="@null" />
我猜这里android:key 是作为参数传递的关键
字符串值 = 首选项.getString(key, "null");
【问题讨论】:
标签: android sharedpreferences android-preferences preferenceactivity