Pull a preference from a xml preference layout
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:开发者_JS百科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.
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:
SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
boolean savedValue = defaultSharedPreferences.getBoolean("randomPref", false);
精彩评论