2010-11-02 36 views
14

Tôi đang cố gắng xóa tùy chọn khỏi màn hình để nó không hiển thị nếu người dùng đang sử dụng SDK lớn hơn 2.2. Tôi đã tìm thấy một số câu trả lời nói rằng getPreferenceScreen(). RemovePreference (thePreference) sẽ làm việc, nhưng tôi nhận được FALSE trả về mỗi khi tôi thử nó. Tôi có sử dụng nó ở sai chỗ không? Bất kỳ đầu mối từ mã của tôi?Cách xóa tùy chọn Android khỏi màn hình

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

Trả lời

45

Được rồi, vấn đề trong trường hợp của tôi là tùy chọn của tôi được phân tách thành các danh mục và khi các tùy chọn được phân loại, bạn không thể đơn giản là ".removePreference" như chúng tôi đang cố gắng thực hiện. đầu tiên tôi đã phải tham khảo các loại có chứa các ưu tiên tôi muốn loại bỏ và sau đó loại bỏ sở thích từ danh mục như vậy ..

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

Tôi giả định này có cái gì để làm với khả năng có nhiều sở thích với cùng (để loại bỏ chính xác). Dù sao thì cảm ơn Vladimir vì đã gắn bó với tôi. Chắc chắn sẽ không tìm thấy câu trả lời mà không có bạn.

+1

Cảm ơn bạn đã giải quyết được vấn đề này cho tôi! Loại bỏ các ưu đãi từ nó PreferenceCategory hoạt động tốt! – stealthcopter

+1

Đã gãi đầu của tôi cho đến khi tôi thấy điều này, không có thắc mắc tất cả mọi thứ không hoạt động sau khi tôi thêm PreferenceCategory! Họ nên ghi lại nó ở đâu đó .. – Bruce

1

Mã có vẻ đúng. Cho bạn biết rằng đoạn mã sau hoạt động, chỉ thử nghiệm nó:

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

}

gì về kiểm tra của bạn cho sdk xây dựng? Liệu mã thực sự đi theo điều kiện?

Và vui lòng kiểm tra tên tùy chọn của bạn. Có thể bạn đã không có nó với phương pháp findPreference.

+0

Cảm ơn phản hồi nhanh của bạn. Có, trước khi đăng tôi thực sự đã có mã in kết quả của việc loại bỏ (mà trả về một boolean) và nó cho tôi một sai lầm. Tôi đã xóa câu lệnh if chỉ để loại bỏ bất kỳ biến nào và tôi cũng đã kiểm tra và thử các tùy chọn khác nhau với cùng một kết quả. Bất kỳ cơ hội nào tôi có thể nhìn thấy toàn bộ lớp học của bạn mà bạn đã thử nghiệm nó? Tôi không biết tại sao nó không làm việc cho tôi. – honeal

+0

Đã chỉnh sửa câu trả lời. –

+0

Cảm ơn dude, đã giúp tấn. Bạn không có cách nào để biết chính xác điều gì đã xảy ra mà không nhìn thấy XML của tôi trước. Cảm ơn một lần nữa. Tôi sẽ quảng bá câu trả lời của bạn nếu tôi ở trên 15. – honeal

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

//Change language 
    setApplicationLanguage(); 

//and rebuild 
    addPreferencesFromResource(R.xml.preferences); 
Các vấn đề liên quan