从 xml 首选项布局中提取首选项
好吧,我已经在这方面工作了一段时间了,我知道这比我做的要容易,但我想做的是我在该布局上有我的 xml 首选项布局或任何你想称呼它的东西我有几个复选框、列表视图项和编辑文本,我需要做的是从 xml 布局获取这些首选项,然后我需要存储它们并在另一个活动中读取它们。我对这些做了一些工作,但是我总是有一个存储字符串的活动,然后我在我需要首选项的活动中扩展了该活动。我知道互联网上有数百个这样的教程,但我只是不明白如何实现它们。到目前为止,这是我的代码。
public class MyPreference extends PreferenceActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
SharedPreferences sharedPreference = getSharedPreferences("PREFERENCES", Activity.MODE_PRIVATE);
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.random_pref);
// Get the custom preference
final Preference randomPref = (Preference) findPreference("randomPref");
Preference randomDayPref = (Preference) findPreference("randomDayPref");
Preference hourPref = (Preference) findPreference("hourPref");
Preference maxRandomMessagePref = (Preference) findPreference("maxRandomMessagePref");
Preference randomContactsPref = (Preference) findPreference("randomContactsPref");
Preference sendSMSPref = (Preference) findPreference("sendSMSPref");
Preference sendEmailPref = (Preference) findPreference("sendEmailPref");
//Finished
randomPref.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick(Preference preference)
{
return true;
}
});
//Finished
randomDayPref.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick(Preference preference)
{
return true;
}
});
hourPref.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick(Preference preference)
{
return true;
}
});
maxRandomMessagePref.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick(Preference preference)
{
return true;
}
});
randomContactsPref.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick(Preference preference)
{
return true;
}
});
sendSMSPref.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick(Preference preference)
{
return true;
}
});
sendEmailPref.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick(Preference preference)
{
return true;
}
});
}
}
的 XML 文件,
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<PreferenceCategory
android:title="Randomization Properties">
<CheckBoxPreference
android:title="Random Delays"
android:summary="Message"
android:defaultValue="false"
android:key="randomPref" />
<CheckBoxPreference
android:title="Throughout The Day"
android:summary="Message"
android:defaultValue="false"
android:key="randomDayPref" />
<ListPreference
android:title="Random Hourly Timings"
android:summary="Message"
android:key="hourPref"
android:defaultValue="6"
android:entries="@array/hours"
android:entryValues="@array/listValues"/>
<EditTextPreference
android:name="Messages"
android:summary="Message"
android:defaultValue="100"
android:title="Messages"
android:key="maxRandomMessagePref" />
</PreferenceCategory>
<PreferenceCategory
android:title="Contacts">
<Preference
android:title="@string/contacts"
android:summary="Message"
android:key="randomContactsPref" />
</PreferenceCategory>
<PreferenceCategory
android:title="Options">
<CheckBoxPreference
android:title="Send Through SMS"
android:summary="Send Messages As Normal"
android:defaultValue="false"
android:key="sendSMSPref" />
<CheckBoxPreference
android:title="Send Trhrough E-Mail"
android:summary="Send Messages Through E-Mail Uses E-Mail Settings"
android:defaultValue="false"
android:key="sendEmailPref" />
</PreferenceCategory>
</PreferenceScreen>
这是我在网上看到 我可能需要创建一个 onPrefernceChangeListener()?我真的对此感到不知所措,我知道它一定很简单,我只是不明白当用户更改首选项时如何从 xml 中提取首选项,然后保存它们,以便我的 man 活动可以读取它们并进行相应调整。如果知道自己在做什么的人可以为我指明正确的方向,那就太好了。
Okay so i have been working on this for a while now, i know this has got to be easier than im making it but what im trying to do is i have my xml preference layout or whatever you would like to call it, on that layout i have several check boxes a list view item and an edit text, what i need to do is get those preferences from the xml layout, then i need to store them and read them in another activity. I have worked a little with these however i always had an activity that stored the strings then i extended that activity within the the activity i need the preferences for. I know there are hundreds of these tutorials all over the internet but i just dont understand how to implement them. Heres my code so far.
public class MyPreference extends PreferenceActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
SharedPreferences sharedPreference = getSharedPreferences("PREFERENCES", Activity.MODE_PRIVATE);
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.random_pref);
// Get the custom preference
final Preference randomPref = (Preference) findPreference("randomPref");
Preference randomDayPref = (Preference) findPreference("randomDayPref");
Preference hourPref = (Preference) findPreference("hourPref");
Preference maxRandomMessagePref = (Preference) findPreference("maxRandomMessagePref");
Preference randomContactsPref = (Preference) findPreference("randomContactsPref");
Preference sendSMSPref = (Preference) findPreference("sendSMSPref");
Preference sendEmailPref = (Preference) findPreference("sendEmailPref");
//Finished
randomPref.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick(Preference preference)
{
return true;
}
});
//Finished
randomDayPref.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick(Preference preference)
{
return true;
}
});
hourPref.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick(Preference preference)
{
return true;
}
});
maxRandomMessagePref.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick(Preference preference)
{
return true;
}
});
randomContactsPref.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick(Preference preference)
{
return true;
}
});
sendSMSPref.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick(Preference preference)
{
return true;
}
});
sendEmailPref.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick(Preference preference)
{
return true;
}
});
}
}
And heres my XML file
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<PreferenceCategory
android:title="Randomization Properties">
<CheckBoxPreference
android:title="Random Delays"
android:summary="Message"
android:defaultValue="false"
android:key="randomPref" />
<CheckBoxPreference
android:title="Throughout The Day"
android:summary="Message"
android:defaultValue="false"
android:key="randomDayPref" />
<ListPreference
android:title="Random Hourly Timings"
android:summary="Message"
android:key="hourPref"
android:defaultValue="6"
android:entries="@array/hours"
android:entryValues="@array/listValues"/>
<EditTextPreference
android:name="Messages"
android:summary="Message"
android:defaultValue="100"
android:title="Messages"
android:key="maxRandomMessagePref" />
</PreferenceCategory>
<PreferenceCategory
android:title="Contacts">
<Preference
android:title="@string/contacts"
android:summary="Message"
android:key="randomContactsPref" />
</PreferenceCategory>
<PreferenceCategory
android:title="Options">
<CheckBoxPreference
android:title="Send Through SMS"
android:summary="Send Messages As Normal"
android:defaultValue="false"
android:key="sendSMSPref" />
<CheckBoxPreference
android:title="Send Trhrough E-Mail"
android:summary="Send Messages Through E-Mail Uses E-Mail Settings"
android:defaultValue="false"
android:key="sendEmailPref" />
</PreferenceCategory>
</PreferenceScreen>
from what i see online i may need to create an onPrefernceChangeListener()? i am really at a loss with this i know its got to be simple i just don't understand how to pull the preferences from the xml when a user changes them and then save them so my man activity can read them and adjust accordingly. If someone who knows what they are doing could point me in the right direction that would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太确定你在这里要问什么,但我会尝试回答。
如果您没有做任何奇特的事情,并且使用默认共享首选项,则无需覆盖任何首选项的
setOnPreferenceClickListener
。基类将为您保存它。要从其他活动访问这些首选项,您将执行以下操作:
I'm not exactly sure what you are asking for here but I'll attempt to answer.
If you aren't doing anything fancy, you won't need to override any of the preference's
setOnPreferenceClickListener
s if you use the Default Shared Preferences. The base class will handle saving it for you.To access these preferences from another activity, you will do something like this: