2012-01-12 58 views
0
public class SettingsActivity extends PreferenceActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    /* Some initializations */ 
    LinearLayout layout = new LinearLayout(this); 
    layout.setOrientation(LinearLayout.VERTICAL); 

    ListView listView = new ListView(this); 
    listView.setId(android.R.id.list); 
    listView.setLayoutParams(new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.FILL_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT, 1)); 
    layout.addView(listView); 

    this.setContentView(layout); 
    /* Preferences time! (we build the preferences) */ 
    Preference version = getPreference("My School Manager", "Version 2.0", 
      null); 
    Preference author = getPreference("Author", "Simone Casagranda", null); 
    Preference marketLink = getPreference("Android market", 
      "View all my apps :)", 
      new Intent(Intent.ACTION_VIEW, Uri 
        .parse("http://market.android.com/details?id=" 
          + "it.trento.alchemiasoft.casagranda.simone"))); 

    CheckBoxPreference check = new CheckBoxPreference(this); 
    check.setTitle("Checkbox"); 
    check.setSummary("Example of checkbox"); 

    DialogPreference license = new MyDialogPreference(this, "License", 
      "This is the license for...bla bla"); 

    /* Now we add the preferences to the preference screen */ 
    PreferenceScreen preferenceScreen = this.getPreferenceManager() 
      .createPreferenceScreen(this); 
    addPreferenceCategory(preferenceScreen, "Preferences Tutorial", 
      version, author, marketLink, check, license); 
    this.setPreferenceScreen(preferenceScreen); 
} 

private boolean addPreferenceCategory(PreferenceScreen preferenceScreen, 
     String titleCategory, Preference... preferences) { 
    boolean addPreference = false; 
    for (Preference preference : preferences) { 
     if (preference != null) 
      addPreference = true; 
    } 
    if (addPreference) { 
     PreferenceCategory preferenceCategory = new PreferenceCategory(this); 
     preferenceCategory.setTitle(titleCategory); 
     preferenceScreen.addPreference(preferenceCategory); 
     for (Preference preference : preferences) { 
      if (preference != null) 
       preferenceCategory.addPreference(preference); 
     } 
     return true; 
    } else 
     return false; 
} 

private Preference getPreference(String title, String summary, Intent intent) { 
    Preference pref = new Preference(this); 
    pref.setTitle(title); 
    pref.setSummary(summary); 
    if (intent != null) 
     pref.setIntent(intent); 
    return pref; 
} 

public class MyDialogPreference extends DialogPreference { 
    public MyDialogPreference(Context context, String title, String text) { 
     super(context, null); 
     this.setTitle(title); 
     this.setDialogMessage(text); 
    } 
} 

}理解PreferenceActivity

的在這一段代碼,偏好列在列表視圖。它不需要適配器嗎?我沒有看到數據被輸入到列表視圖中。這個首選項屏幕怎麼樣?是否需要設置偏好?

回答

1

PreferenceActivity自行處理偏好。該適配器內置於PreferenceActivity中,以便您不必擔心。而不是以編程方式創建您的首選項,您可以使用XML來執行相同的操作。檢查this tutorial

偏好活動消除了很多痛苦。想象一下,編寫用於連接所有UI操作的代碼,如顯示對話框,更改首選項的狀態並保存它們。偏好活動會讓你遠離痛苦。

+0

好吧,從上面,什麼是數據源進入列表視圖? PreferenceScreen,PreferenceCategory,Preference ...等等嗎? – lilzz 2012-01-12 06:17:58

+0

所有的3 :),每個項目呈現不同,並按給定的順序。 – 2012-01-12 06:23:52