The android SDK provides several preference components, such as checkboxpreference, edittextpreference, dialogpreference, and listpreference. These components are bound to the preference storage mechanism provided by Android, you can use these components to modify some application configurations, as shown in figure. This is the built-in system setting interface of Android:
However, these components cannot meet the requirements of 100%. Suppose we need to provide an application with a setting to select different images as the background image of the application, we need a very intuitive view of the selected image, and then click to browse other images and select. Therefore, the preference cannot meet this requirement. Therefore, we need to extend the preference, which is the result of the extension:
See the effect of the intermediate options. The selected image is displayed on the right.
The Code is as follows:
Source code printing?
- Import Android. content. context;
- Import Android. content. intent;
- Import Android. OS. Bundle;
- Import Android. Preference. preference;
- Import Android. Preference. preferenceactivity;
- Import Android. util. attributeset;
- Import Android. View. view;
- Import Android. widget. imageview;
- /**
- * Image option, used to set the image and border
- * @ Author winter Lau
- */
- Public class imageoptionpreference extends preference {
- Private preferenceactivity parent;
- Private int mimage = R. drawable. CAR;
- Private imageview preview_img;
- Public imageoptionpreference (context, attributeset attrs, int defstyle ){
- Super (context, attrs, defstyle );
- }
- Public imageoptionpreference (context, attributeset attrs ){
- Super (context, attrs );
- }
- Public imageoptionpreference (context ){
- Super (context );
- }
- Void setactivity (preferenceactivity parent ){
- This. Parent = parent;
- }
- @ Override
- Public Boolean ispersistent (){
- Return false;
- }
- /**
- * Modify an image
- * @ Param newimage
- * @ Return
- */
- Boolean changegamepic (INT newimage ){
- If (this. mimage = newimage)
- Return false;
- Gameglobal. save_pic (newimage );
- This. mimage = newimage;
- Preview_img.setimageresource (newimage );
- Return true;
- }
- @ Override
- Protected void onbindview (view ){
- Super. onbindview (View );
- This. mimage = gameglobal. get_pic ();
- Preview_img = (imageview) view. findviewbyid (R. Id. pref_current_img );
- Preview_img.setimageresource (this. mimage );
- }
- @ Override
- Protected void onclick (){
- Super. onclick ();
- Bundle bundle = new bundle ();
- Bundle. putint (gameglobal. pref_key_image, this. mimage );
- Intent intent = new intent (parent, imageselector. Class );
- Intent. putextras (bundle );
- Parent. startactivityforresult (intent, magicsetting. request_code_game_image );
- }
- }
Import android. content. context; import android. content. intent; import android. OS. bundle; import android. preference. preference; import android. preference. preferenceactivity; import android. util. attributeset; import android. view. view; import android. widget. imageview;/*** image option, used to set the image and border * @ author winter Lau */public class imageoptionpreference extends preference {private preferenceactivity parent; private int mimage = R. drawable. car; private imageview preview_img; Public imageoptionpreference (context, attributeset attrs, int defstyle) {super (context, attrs, defstyle);} public imageoptionpreference (context, attributeset attrs) {super (context, attrs);} public imageoptionpreference (context) {super (context);} void setactivity (preferenceactivity parent) {This. parent = parent ;}@ overridepublic Boolean ispersistent () {return false;}/*** modify image * @ Param newimage * @ return */Boolean changegamepic (INT newimage) {If (this. mimage = newimage) return false; gameglobal. save_pic (newimage); this. mimage = newimage; preview_img.setimageresource (newimage); Return true ;}@ overrideprotected void onbindview (view) {super. onbindview (View); this. mimage = gameglobal. get_pic (); preview_img = (imageview) view. findviewbyid (R. id. pref_current_img); preview_img.setimageresource (this. mimage) ;}@ override protected void onclick () {super. onclick (); bundle = new bundle (); bundle. putint (gameglobal. pref_key_image, this. mimage); intent = new intent (parent, imageselector. class); intent. putextras (bundle); parent. startactivityforresult (intent, magicsetting. request_code_game_image );}}
The perference configuration information is as follows:
<Com. liusoft. Android. fmagic. imageoptionpreference
Android: Key = "game_pic"
Android: Persistent = "false"
Android: Title = "@ string/pref_pic_title"
Android: Summary = "@ string/pref_pic_summary"
Android: widgetlayout = "@ layout/preference_widget_image"
/>
The preference_widget_image information is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<! -- Layout used by imageoptionpreference for the image option style.
This is inflated inside Android. R. layout. Preference.
-->
<Imageview xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Id = "@ + ID/pref_current_img"
Android: layout_width = "54dip"
Android: layout_height = "54dip"
Android: layout_marginright = "4dip"
Android: layout_gravity = "center_vertical"
Android: focusable = "false"
Android: clickable = "false"
Android: Background = "# eeeeee"
Android: padding = "2dip"
/>
The layout of this imageview is the image displayed on the right of the option.