使用Android首选项,不向用户显示变量
我有一个SharedReferences实现,在其中存储用户首选项。当用户单击菜单中的“首选项”时,我打开标准编辑器,允许用户更改首选项。代码如下:使用Android首选项,不向用户显示变量,android,variables,hide,preferences,Android,Variables,Hide,Preferences,我有一个SharedReferences实现,在其中存储用户首选项。当用户单击菜单中的“首选项”时,我打开标准编辑器,允许用户更改首选项。代码如下: @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.MENU_PREFERENCES: Intent intent = new Intent(context, Pr
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.MENU_PREFERENCES:
Intent intent = new Intent(context, PreferencesActivity.class);
startActivity(intent);
return true;
case xx:
....
}
Toast.makeText(this, "ERROR: Bad menu item", Toast.LENGTH_SHORT).show();
return true;
}
在PreferencesActivity.java中,我有:
public class PreferencesActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
我的问题是,我的首选项中是否可以有一个变量供内部使用,我的意思是,当启动触觉(意图)时,它不会显示给用户?怎么做
我想我必须更改preferences.xml中的某些内容,但不知道具体是什么
谢谢
编辑:根据请求,我粘贴xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory>
<ListPreference android:title="Pomodoro duration" android:key="pomodoro_duration" android:summary="Duration of each pomodoro" android:entryValues="@array/array_duration_values" android:defaultValue="25" android:entries="@array/array_duration"/><ListPreference android:key="short_break_duration" android:title="Short break duration" android:summary="Duration of break after each pomodoro" android:entryValues="@array/array_duration_values" android:entries="@array/array_duration" android:defaultValue="5"/>
<ListPreference android:title="Long break interval" android:summary="Interval of the longer break" android:key="intervalos" android:entryValues="@array/interval_array_values" android:defaultValue="4" android:entries="@array/interval_array"/>
<ListPreference android:defaultValue="20" android:title="Long break duration" android:summary="Duration of break after each pomodoro" android:key="long_break_duration" android:entries="@array/array_duration" android:entryValues="@array/array_duration_values"/><RingtonePreference android:title="Notification sound" android:ringtoneType="notification" android:summary="Tone that will sound after pomodoro ends" android:key="notification_tone" android:showDefault="true" android:showSilent="true"/>
<ListPreference android:summary="Period used to calculate productivity" android:title="Calculus period" android:key="calculus_period" android:entryValues="@array/array_calculo_valores" android:entries="@array/array_calculo"/>
</PreferenceCategory>
</PreferenceScreen>
当我修改标准UI以允许用户设置此变量时,我想在编程时隐藏其中一个变量。可能吗?如果它不在preferences.xml中,它将不会显示,所以不要将它添加到那里。您可以使用
SharedReferences
类来获取/设置首选项,而无需显示UI。比如:
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
boolean flag = prefs.getBoolean("flag", false);
根本无法理解你的问题。用户如何看到系统的任何变量???我想他是说他想在首选项中设置一个变量,而不显示它的UI元素。。。你不能正常地分配变量吗?为什么你必须在首选项中这样做?有点混乱。将xml代码粘贴到这里…?对于获取变量,这不是我想要的。我想要的是隐藏显示给用户的UI菜单中的一个首选项,以允许用户选择新值。我在我的原始帖子中用startActivity(意图)展示了这一点。你的原始帖子不清楚。您可以获取并设置如上所示的首选项,而无需显示UI。你不必隐藏它,只是首先不要把它放在UI中。也许我有相同的错误概念,或者我错了。我会尽量解释清楚:我不想设置或获取变量。此外,我也没有做一个编程的首选用户界面。我所做的是调用startactivity(intent)和android管理向用户显示配置GUI以更改偏好。也就是说,我想要的是有一个动态首选项选项,它是否显示取决于某些应用程序逻辑条件。用这种型号可以吗?或者我必须用我想要的逻辑开发我自己的偏好GUI吗?好的,这更清楚了。您实际上无法隐藏它,但可以禁用它。使用
PreferenceActivity.findReference()
获取引用a首选项
,调用setEnabled(true | false)
根据应用程序的状态启用/禁用它。