2011-10-25 45 views
0

我做了一個簡單的擴展CheckBoxPreference,這樣我就可以在標題左側有一個圖像圖標的自定義視圖。代碼如下:setOnPreferenceChangeListener不能用於自定義複選框首選項

public class CustomCheckBoxPreference extends CheckBoxPreference { 

private Drawable icon; 

public CustomCheckBoxPreference(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.CustomCheckBoxPreference, 0, 0); 
    icon = arr.getDrawable(R.styleable.CustomCheckBoxPreference_icon); 
    setLayoutResource(R.layout.custom_checkbox_pref); 
} 

@Override 
protected void onBindView(View view) { 
    super.onBindView(view); 
    ImageView prefsIcon = (ImageView) view.findViewById(R.id.prefs_icon); 
    prefsIcon.setImageDrawable(icon); 
} 

的問題是,由於某種原因,我OnPreferenceChangeListener設置爲任何CustomCheckboxPreference沒有效果,而不是存儲。我試着重寫一些調用super的實現的android方法,然後打印一行來查看調用的內容。值得注意的是callChangeListener沒有被調用。正是這種方法導致onPreferenceChanged的回調。我試着打電話拋下onPreferenceChanged內setChecked的只是爲了看看會發生什麼和OnPreferenceChangeListener爲空:

  getOnPreferenceChangeListener().onPreferenceChange(this, checked); 

這是我如何設置preferencechangelistener:

 mTwitterPref.setChecked(!mTwitter.needsAuth()); 
    mTwitterPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 
     @Override 
     public boolean onPreferenceChange(Preference preference, Object newValue) { 
      System.out.println("Twitter Preference Changed!"); 
      if ((Boolean) newValue) { 
       if (mTwitter.needsAuth()) { 
        System.out.println("We Need To Login To Twitter!"); 
        IntentUtils.startActivityForResult(ProfileAccountsActivity.this, 
          TwLoginActivity.class, ACTIVITY_OAUTH); 
       } 
      } else { 
       showDialog(DIALOG_LOGOUT_TWITTER); 
      } 
      return false; 
     } 
    }); 

我有點困惑至於爲什麼preferencechangelistener不能正常工作,因爲我只覆蓋了onBindView和構造函數;我都叫超級。有什麼想法嗎?

+0

您希望在複選框被選中時更新前導?你不需要在你的複選框上設置onCheckChangedListener? – LuxuryMode

回答

4

設置機器人:可調焦= 「假」 和android:點擊=上的複選框 「假」:

<CheckBox 
    android:id="@+android:id/checkbox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:focusable="false" 
    android:clickable="false" /> 

在此線程更多信息:Clickable rows on custom ArrayAdapter

0

我有自定義按鈕同樣的問題。我試着@jeanh提供的解決方案,它的工作原理。但我的按鈕沒有按下,只有周圍的區域被突出顯示。而且,如果你有幾個按鈕呢?很明顯,這個解決方案是行不通的。所以,我決定深入挖掘和我的解決方案低於:

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/widget_frame" android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:paddingLeft="15dp" 
android:paddingTop="5dp" 
android:paddingRight="10dp" 
android:paddingBottom="5dp"  
> 
<!-- define my button --> 
<Button android:id="@android:id/title" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="17sp" 
     android:typeface="sans" 
     android:textStyle="bold" 
     android:textColor="#000000" 
></Button>  

</RelativeLayout> 

Java類:

public class ButtonPreference extends Preference { 

    private final String TAG = getClass().getName(); 

    public interface ButtonListener {  
     public void onCustomClick(); 
    } 

    public ButtonListener buttonListener; 

    public void setButtonListener(ButtonListener buttonListener){ 
     this.buttonListener = buttonListener; 
    } 

    public ButtonPreference(Context context, AttributeSet attrs) { 
     super(context, attrs);  
    } 

    public ButtonPreference(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle);   
    } 

    @Override 
    protected View onCreateView(ViewGroup parent){ 

     RelativeLayout layout = null; 

     try { 
      LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      layout = (RelativeLayout)mInflater.inflate(R.layout.button_preference, parent, false); 
      //FIND OUR BUTTON IN LAYOUT 
      Button button = (Button) layout.findViewById(android.R.id.title); 
      if(button!=null){ 
      Log.e(TAG, "button found"); 
      button.setOnClickListener(new OnClickListener() {     
       @Override 
       public void onClick(View v) {      
        if(buttonListener!=null){       
         buttonListener.onCustomClick(); //INVOKE OUR EVENT! 
        } 
       } 
      }); 
      }    
     } 
     catch(Exception e) 
     { 
      Log.e(TAG, "Error creating info preference", e); 
     }   
     return layout;   
    }  
} 

如何使用它?簡單!

public class WallpaperSettings extends PreferenceActivity { 

protected void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState); 
    addPreferencesFromResource(R.xml.prefs);  

    ButtonPreference defaultSettingsButton = (ButtonPreference) findPreference(EngineCore.pref+"defaultSettings");  
    defaultSettingsButton.setButtonListener(new ButtonListener() {   
     @Override 
     public void onCustomClick() { 
      Gdx.app.log("ButtonListener", "onCustomClick");    
     } 
    }); 
} 
} 

我希望它可以幫助別人。

0

我找到解決方案! 就我而言,我伸出DialogPreference到我的自定義對話框類,

public class SvDialogPreference extends DialogPreference 

我也迷惑,因爲在PreferenceFragment,onPreferenceChange從來沒有工作過。

SvDialogPreference pref= (SvDialogPreference) findPreference("mainKey"); 
if(pref != null) { 
pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 
@Override 
public boolean onPreferenceChange(Preference preference, Object newValue) { 
// Never execute ! 
}} 

解決此問題。我在onDialogClosed中調用了「super.callChangeListener」。

public class SvDialogPreference extends DialogPreference{ 
.... 
@Override 
protected void onDialogClosed(boolean positiveResult) { 

double InputValue = Double.parseDouble(KEY.getText().toString()); 
    super.callChangeListener(InputValue); 
    super.onDialogClosed(positiveResult); 
} 

現在,onPreferenceChange工作正常!