Compile a custom Android preference component

Source: Internet
Author: User

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?
  1. Import Android. content. context;
  2. Import Android. content. intent;
  3. Import Android. OS. Bundle;
  4. Import Android. Preference. preference;
  5. Import Android. Preference. preferenceactivity;
  6. Import Android. util. attributeset;
  7. Import Android. View. view;
  8. Import Android. widget. imageview;
  9. /**
  10. * Image option, used to set the image and border
  11. * @ Author winter Lau
  12. */
  13. Public class imageoptionpreference extends preference {
  14. Private preferenceactivity parent;
  15. Private int mimage = R. drawable. CAR;
  16. Private imageview preview_img;
  17. Public imageoptionpreference (context, attributeset attrs, int defstyle ){
  18. Super (context, attrs, defstyle );
  19. }
  20. Public imageoptionpreference (context, attributeset attrs ){
  21. Super (context, attrs );
  22. }
  23. Public imageoptionpreference (context ){
  24. Super (context );
  25. }
  26. Void setactivity (preferenceactivity parent ){
  27. This. Parent = parent;
  28. }
  29. @ Override
  30. Public Boolean ispersistent (){
  31. Return false;
  32. }
  33. /**
  34. * Modify an image
  35. * @ Param newimage
  36. * @ Return
  37. */
  38. Boolean changegamepic (INT newimage ){
  39. If (this. mimage = newimage)
  40. Return false;
  41. Gameglobal. save_pic (newimage );
  42. This. mimage = newimage;
  43. Preview_img.setimageresource (newimage );
  44. Return true;
  45. }
  46. @ Override
  47. Protected void onbindview (view ){
  48. Super. onbindview (View );
  49. This. mimage = gameglobal. get_pic ();
  50. Preview_img = (imageview) view. findviewbyid (R. Id. pref_current_img );
  51. Preview_img.setimageresource (this. mimage );
  52. }
  53. @ Override
  54. Protected void onclick (){
  55. Super. onclick ();
  56. Bundle bundle = new bundle ();
  57. Bundle. putint (gameglobal. pref_key_image, this. mimage );
  58. Intent intent = new intent (parent, imageselector. Class );
  59. Intent. putextras (bundle );
  60. Parent. startactivityforresult (intent, magicsetting. request_code_game_image );
  61. }
  62. }
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.