Is it possible to use a normal Activity
as a preference Activity
?
I know you can do this with preference Activity
, but what i am trying to do is make a layout which contains a CheckBox
, Buttons
, and/or a ListView
. So basically a normal layout in xml.
But is it possible to have those items be preferences sort of like they do in a preference screen? So overall i am trying to make a preferences in an Activity
from a layout.
If you read the documentation you would have found: PreferenceActivity with this available you have NO reason to use a Activity. While this is still doable, its not recommended, use what is available.
Yes, it is possible. you can directly read and write values from preference xml without preference activity
For reading preference xml values, you can use code like given below
int value = this.getSharedPreferences("PreferenceFileName", MODE_PRIVATE).getInt("PreferenceName", 1);
to write value to preference xml value, use some thing similar to this
Editor e = this.getSharedPreferences("PreferenceFileName", Context.MODE_PRIVATE).edit();
e.putInt("PreferenceName", newvalue );
e.commit();
you need to read the values when activity is shown and save it when leaving activity. But if you are looking for easiest way to save preference then use preference activity.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.