閱讀133 返回首頁    go 阿裏雲 go 技術社區[雲棲]


編寫自定義的 Android Preference 組件

 

Android SDK 提供好幾個 Preference 組件,例如 CheckBoxPreference、EditTextPreference、DialogPreference、ListPreference 等,這些組件是跟 Android 提供的 Preference 存儲機製綁定的,你可以通過這些組件來修改應用的一些配置,如下圖所示,這是 Android 自帶的係統設置界麵:

 

但這些組件畢竟還不能滿足100%的要求,假設我們需要為應用程序提供一個選擇不同圖片做為應用背景圖的設置,我們需要一個很直觀的就可以看到當前所選擇的圖片,然後點擊後可以瀏覽其他圖片並選擇。那麼這些 Preference 就無法滿足這個需求,因此我們需要對 Preference 進行擴展,下圖是擴展後的效果:

請看中間選項的效果,在右邊顯示當前選擇的圖片。

代碼如下:

 

  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. /**  
  11.  * 圖片選項,用於設置圖片和邊框  
  12.  * @author Winter Lau  
  13.  */   
  14. public   class  ImageOptionPreference  extends Preference {  
  15.   
  16.     private  PreferenceActivity parent;  
  17.     private   int  mImage = R.drawable.car;  
  18.     private  ImageView preview_img;  
  19.       
  20.     public  ImageOptionPreference(Context context, AttributeSet attrs, int  defStyle) {  
  21.         super (context, attrs, defStyle);  
  22.     }  
  23.   
  24.     public  ImageOptionPreference(Context context, AttributeSet attrs) {  
  25.         super (context, attrs);  
  26.     }  
  27.   
  28.     public  ImageOptionPreference(Context context) {  
  29.         super (context);  
  30.     }  
  31.       
  32.     void  setActivity(PreferenceActivity parent) {  
  33.         this .parent = parent;  
  34.     }  
  35.       
  36.     @Override   
  37.     public   boolean  isPersistent() {  
  38.         return   false ;  
  39.     }  
  40.   
  41.     /**  
  42.      * 修改圖片  
  43.      * @param newImage  
  44.      * @return  
  45.      */   
  46.     boolean  ChangeGamePic( int  newImage ){  
  47.         if ( this .mImage == newImage)  
  48.             return   false ;  
  49.         GameGlobal.save_pic(newImage);  
  50.         this .mImage = newImage;  
  51.         preview_img.setImageResource(newImage);  
  52.         return   true ;  
  53.     }  
  54.   
  55.     @Override   
  56.     protected   void  onBindView(View view) {  
  57.         super .onBindView(view);  
  58.           
  59.         this .mImage = GameGlobal.get_pic();  
  60.         preview_img = (ImageView)view.findViewById(R.id.pref_current_img);  
  61.         preview_img.setImageResource(this.mImage);  
  62.     }     
  63.   
  64.     @Override   
  65.     protected   void  onClick() {  
  66.         super .onClick();  
  67.         Bundle bundle = new  Bundle();  
  68.         bundle.putInt(GameGlobal.PREF_KEY_IMAGE, this.mImage);  
  69.         Intent intent = new  Intent(parent, ImageSelector. class );  
  70.         intent.putExtras(bundle);  
  71.         parent.startActivityForResult(intent, MagicSetting.REQUEST_CODE_GAME_IMAGE);          
  72.     }  
  73.   
  74. }  

 

  1. 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;
  2. /**
     * 圖片選項,用於設置圖片和邊框
     * @author Winter Lau
     */
    public class ImageOptionPreference extends Preference {
  3.  private PreferenceActivity parent;
     private int mImage = R.drawable.car;
     private ImageView preview_img;
     
     public ImageOptionPreference(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
     }
  4.  public ImageOptionPreference(Context context, AttributeSet attrs) {
      super(context, attrs);
     }
  5.  public ImageOptionPreference(Context context) {
      super(context);
     }
     
     void setActivity(PreferenceActivity parent) {
      this.parent = parent;
     }
     
     @Override
     public boolean isPersistent() {
      return false;
     }
  6.  /**
      * 修改圖片
      * @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;
     }
  7.  @Override
     protected void onBindView(View view) {
      super.onBindView(view);
      
      this.mImage = GameGlobal.get_pic();
      preview_img = (ImageView)view.findViewById(R.id.pref_current_img);
      preview_img.setImageResource(this.mImage);
     } 
  8.     @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);       
        }
  9. }

 

對應的 Perference 配置信息如下

<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"
    />

而 preference_widget_image 的信息如下

<?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:andro
    android:
    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"
    />
而這個 ImageView 的 Layout 就是在選項右邊顯示的圖片。

最後更新:2017-04-02 06:51:49

  上一篇:go Android開發退出提醒窗口
  下一篇:go Android PreferenceActivity 學習筆記