2010-11-02 51 views
14

我試圖從屏幕中刪除偏好設置,以便在用戶使用的SDK大於2.2時不可見。我發現了幾個答案,說getPreferenceScreen()。removePreference(thePreference)會工作,但我每次嘗試時都會返回FALSE。我是否在錯誤的地方使用它?我的代碼的任何線索?如何從屏幕中刪除Android偏好設置

public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener { 

private static final String POLLING_PREFERENCE = "update_frequency_list"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Load the preferences from an XML resource 
    addPreferencesFromResource(R.xml.preferences); 

    // Get a reference to the preferences 
    mPollPref = getPreferenceScreen().findPreference(POLLING_PREFERENCE); 

    //If the SDK is 2.2 or greater do not display polling preferences (using C2DM instead) 
    if(Build.VERSION.SDK_INT > 7) { 
     getPreferenceScreen().removePreference(mPollPref); 
    } 
} 
.... 
} 

回答

45

好的問題在我的情況是,我的喜好被分爲幾類,當偏好被分類時,你不能簡單地「.removePreference」就像我們正在嘗試做的那樣。我首先要引用包含我想刪除,然後刪除該類別等,以使優先級,優先類別..

public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener { 

private static final String POLLING_PREFERENCE = "update_frequency_list"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

// Load the preferences from an XML resource 
addPreferencesFromResource(R.xml.preferences); 

// Get a reference to the preferences 
mPollPref = getPreferenceScreen().findPreference(POLLING_PREFERENCE); 

//If the SDK is 2.2 or greater do not display polling preferences (using C2DM instead) 
if(Build.VERSION.SDK_INT > 7) { 
    PreferenceCategory notificationsCategory = (PreferenceCategory) findPreference("notifications_category"); 
    notificationsCategory.removePreference(mPollPref); 
} 
} 
.... 
} 

我想這已經是與以具有相同的多個偏好的能力鍵(爲了刪除正確的)。無論如何謝謝弗拉基米爾堅持與我。沒有你,肯定不會找到答案。

+1

謝謝你解決了我的問題!從PreferenceCategory中移除偏好效果很好! – stealthcopter 2011-01-30 22:17:24

+1

抓我的頭,直到我看到這個,難怪一切都失敗後,我添加PreferenceCategory!他們應該在某處記錄下來.. – Bruce 2013-12-09 13:29:13

1

代碼似乎是正確的。告訴你,下面的代碼工作,只是測試它:

package com.lid.ps.screens.edit_activity; 

import android.app.AlertDialog; 
import android.app.DatePickerDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.preference.*; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.*; 
import com.lid.ps.bean.AbstractData; 
import com.lid.ps.bean.Action; 
import com.lid.ps.bean.ActionFactory; 
import com.lid.ps.bean.CommonAlertDialog; 
import com.lid.ps.bean.utils.DateTools; 
import com.lid.ps.model.Activity; 
import com.lid.ps.model.ActivityImpl; 
import com.lid.ps.screens.R; 

import java.util.Calendar; 

/** 
* User: Vladimir Ivanov 
* Date: 04.07.2010 
* Time: 9:58:17 
*/ 
public class EditActivityScreen extends PreferenceActivity 
    implements DatePickerDialog.OnDateSetListener, AbstractData<Activity> { 

private static final int MENU_DELETE_ACTIVITY = 0; 

private boolean create = true; 

public boolean isCreate() { 
    return create; 
} 

private Activity dataContainer; 

private EditTextPreference activityNamePref; 
private EditTextPreference activityDescPref; 
private Preference startDatePref; 
private CheckBoxPreference hiddenPref; 

private int year; 
private int monthOfYear; 
private int dayOfMonth; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    addPreferencesFromResource(R.xml.activity_prefs); 
    //this.setContentView(R.layout.edit_screen); 

    activityNamePref = (EditTextPreference) findPreference("name"); 

    activityDescPref = (EditTextPreference) findPreference("desc");   

    // We have to do this to get the save/cancel buttons to highlight on 
    // their own. 
    getListView().setItemsCanFocus(true); 

    // Grab the content view so we can modify it. 
    FrameLayout content = (FrameLayout) getWindow().getDecorView() 
      .findViewById(android.R.id.content); 

    // Get the main ListView and remove it from the content view. 
    ListView lv = getListView(); 
    content.removeView(lv); 

    // Create the new LinearLayout that will become the content view and 
    // make it vertical. 
    LinearLayout ll = new LinearLayout(this); 
    ll.setOrientation(LinearLayout.VERTICAL); 

    // Have the ListView expand to fill the screen minus the save/cancel 
    // buttons. 
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.FILL_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT); 
    lp.weight = 1; 
    ll.addView(lv, lp); 

    // Inflate the buttons onto the LinearLayout. 
    View v = LayoutInflater.from(this).inflate(
      R.layout.edit_screen, ll); 

    startDatePref = findPreference("time"); 

    hiddenPref = (CheckBoxPreference) findPreference("hidden"); 

    // Initialize buttons 
    Button cancelButton = (Button) v.findViewById(R.id.cancel_activity); 
    Button saveButton = (Button) v.findViewById(R.id.save_activity); 

    cancelButton.setOnClickListener(new CancelOnClickListener()); 
    saveButton.setOnClickListener(new SaveOnClickListener()); 

    setContentView(ll); 

    // if edit mode... 
    if (getIntent() != null && getIntent().getExtras() != null) { 
     // some stuff 
} 

@Override 
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, 
            Preference preference) { 
    if (preference == startDatePref) { 
     new DatePickerDialog(this, this, year, monthOfYear, dayOfMonth).show(); 
    } 

    return super.onPreferenceTreeClick(preferenceScreen, preference); 
} 

private void popAlarmSetToast(Context context) { 
    String toastText;  
    //... 
    final Toast toast = Toast.makeText(context, toastText, Toast.LENGTH_LONG); 
    toast.show(); 
} 

/** 
* updates the preference summary. 
* 
* @param datePicker date picker 
* @param i   year 
* @param i1   month 
* @param i2   day 
*/ 
@Override 
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) { 
    year = i; 
    monthOfYear = i1; 
    dayOfMonth = i2; 
    updateDate(); 
} 

private void updateDate() { 
    startDatePref.setSummary(
      new StringBuilder() 
        // Month is 0 based so add 1 
        .append(year).append("-") 
        .append(monthOfYear + 1).append("-") 
        .append(dayOfMonth).append(" ")); 
} 


private class CancelOnClickListener implements View.OnClickListener { 

    public void onClick(View view) { 
     // go back to the previous page 
     final Action action = ActionFactory.createAction("back_edit_activity"); 
     if (action != null) { 
      action.doAction(EditActivityScreen.this); 
     } 
    } 
} 

private class SaveOnClickListener implements View.OnClickListener { 

    public void onClick(View view) { 

     // validation 
     if (activityNamePref.getSummary() == null || activityNamePref.getSummary().length() == 0) { 
      AlertDialog dialog = new AlertDialog.Builder(EditActivityScreen.this).create(); 
      dialog.setTitle(R.string.validation_failed); 
      dialog.setMessage(EditActivityScreen.this.getResources().getString(R.string.activity_create_fail)); 
      dialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 
       } 
      }); 
      dialog.show(); 
     } else { 
      final Action action = ActionFactory.createAction("save_edit_activity"); 
      if (action != null) { 
       action.doAction(EditActivityScreen.this); 
      } 
      popAlarmSetToast(EditActivityScreen.this); 
     } 
    } 
} 

}

那你的SDK支票建?代碼是否真的符合條件?

請檢查您的偏好名稱。可能是你沒有用findPreference方法得到它。

+0

感謝您的快速響應。是的,在發佈之前,我實際上有代碼打印刪除的結果(它返回一個布爾值),它給了我一個錯誤。爲了消除任何變量,我刪除了if語句,並且還檢查了不同的首選項,並嘗試了相同的結果。任何機會我都能看到你的整個班級,你測試過嗎?我不知道爲什麼它不適合我。 – honeal 2010-11-02 20:59:09

+0

編輯答案。 – 2010-11-03 07:56:42

+0

謝謝兄弟,幫助噸。如果不先看到我的XML,你無法知道究竟發生了什麼。再次感謝。我會促進你的答案,如果我高於15. – honeal 2010-11-03 15:51:59

4
//remove all preferences 
    mPollPref = getPreferenceScreen(); 
    mPollPref.removeAll(); 

//Change language 
    setApplicationLanguage(); 

//and rebuild 
    addPreferencesFromResource(R.xml.preferences);