天天看點

編寫自定義的 Android Preference 元件

android sdk 提供好幾個 preference 元件,例如 checkboxpreference、edittextpreference、dialogpreference、listpreference 等,這些元件是跟 android 提供的 preference 存儲機制綁定的,你可以通過這些元件來修改應用的一些配置,如下圖所示,這是 android 自帶的系統設定界面:

但這些元件畢竟還不能滿足100%的要求,假設我們需要為應用程式提供一個選擇不同圖檔做為應用背景圖的設定,我們需要一個很直覺的就可以看到目前所選擇的圖檔,然後點選後可以浏覽其他圖檔并選擇。那麼這些 preference 就無法滿足這個需求,是以我們需要對 preference 進行擴充,下圖是擴充後的效果:

編寫自定義的 Android Preference 元件

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

代碼如下:

源碼列印

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;  

/**  

 * 圖檔選項,用于設定圖檔和邊框  

 * @author winter lau  

 */   

public  

class  imageoptionpreference  extends preference {  

    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);  

    }  

    public  imageoptionpreference(context context, attributeset attrs) {  

        super (context, attrs);  

    public  imageoptionpreference(context context) {  

        super (context);  

    void  setactivity(preferenceactivity parent) {  

        this .parent = parent;  

    @override   

    public  

boolean  ispersistent() {  

        return  

false ;  

    /**  

     * 修改圖檔  

     * @param newimage  

     * @return  

     */   

    boolean  changegamepic(

int  newimage ){  

        if (

this .mimage == newimage)  

            return  

        gameglobal.save_pic(newimage);  

        this .mimage = newimage;  

        preview_img.setimageresource(newimage);  

true ;  

    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);  

    }     

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;

/**

 * 圖檔選項,用于設定圖檔和邊框

 * @author winter lau

 */

public class imageoptionpreference extends preference {

 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);

 }

 public imageoptionpreference(context context, attributeset attrs) {

  super(context, attrs);

 public imageoptionpreference(context context) {

  super(context);

 void setactivity(preferenceactivity parent) {

  this.parent = parent;

 @override

 public boolean ispersistent() {

  return false;

 /**

  * 修改圖檔

  * @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;

 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);

 } 

    @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);       

    }

}

對應的 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: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"

而這個 imageview 的 layout 就是在選項右邊顯示的圖檔。