Android 程式開發:(十三)特殊片段 —— 13.3 PreferenceFragment

來源:互聯網
上載者:User

有的時候,我們做的程式需要提供一些選項的功能,能讓使用者去定製化他們自己的使用風格。舉個例子,你可能允許使用者是否自動儲存登入資訊,允許使用者自己設定某個頁面的重新整理時間等等。在Android平台上面,我們可以使用PreferenceActivity基類去顯示給使用者一個選項設定的介面。在Android3.0或更高的版本上,可以使用PreferenceFragment類去實現這個功能。

下面將展示如何去建立和使用PreferenceFragment。

1、建立一個工程:PreferenceFragmentExample。

2、在res檔案夾下面建立一個xml檔案夾,在xml檔案夾下面建立一個檔案:preferences.xml。


[html] 
<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen  
    xmlns:android="http://schemas.android.com/apk/res/android"> 
 
    <PreferenceCategory android:title="Category 1"> 
        <CheckBoxPreference 
            android:title="Checkbox" 
            android:defaultValue="false" 
            android:summary="True of False" 
            android:key="checkboxPref" /> 
        </PreferenceCategory>                 
         
    <PreferenceCategory android:title="Category 2"> 
        <EditTextPreference 
            android:name="EditText" 
            android:summary="Enter a string" 
            android:defaultValue="[Enter a string here]" 
            android:title="Edit Text" 
            android:key="editTextPref" />             
        <RingtonePreference 
            android:name="Ringtone Preference" 
            android:summary="Select a ringtone" 
            android:title="Ringtones" 
            android:key="ringtonePref" />             
        <PreferenceScreen                 
            android:title="Second Preference Screen" 
            android:summary= 
                "Click here to go to the second Preference Screen" 
            android:key="secondPrefScreenPref">                             
            <EditTextPreference 
                android:name="EditText" 
                android:summary="Enter a string" 
                android:title="Edit Text (second Screen)" 
                android:key="secondEditTextPref" />                 
        </PreferenceScreen>         
    </PreferenceCategory>   
           
</PreferenceScreen> 
3、在包路徑下面建立一個類:Fragment1.java。
[html] 
public class Fragment1 extends PreferenceFragment { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
 
        // 從xml檔案載入選項 
        addPreferencesFromResource(R.xml.preferences); 
    } 

4、PreferenceFragmentExampleActivity.java(主活動)的代碼。
[java] 
public class PreferenceFragmentExampleActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        FragmentManager fragmentManager = getFragmentManager(); 
        FragmentTransaction fragmentTransaction =  
            fragmentManager.beginTransaction(); 
        Fragment1 fragment1 = new Fragment1(); 
        fragmentTransaction.replace(android.R.id.content, fragment1);         
        fragmentTransaction.addToBackStack(null);  
        fragmentTransaction.commit(); 
    } 

5、按F11在模擬器上面調試。

6、點擊第二行,就會顯示一個視窗。

 

7、使用DDMS工具,就會看見在本應用的包路徑下面會產生一個xml檔案。

 


如果想建立一個列表樣式的選項,首先,我們需要先建立preferences.xml檔案,然後在這個檔案裡面填充各種元素。

然後,我們需要一個繼承PreferenceFragment的子類:

[html] 
public class Fragment1 extends PreferenceFragment {} 
接下來,我們使用addPreferencesFromResource()方法去載入xml檔案:
[html] 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
 
        // 從xml檔案載入選項 
        addPreferencesFromResource(R.xml.preferences); 
    } 
最後,我們要把這個Fragment1顯示在活動中,這時,我們需要使用FragmentManager和FragmentTrasaction:
[java] 
FragmentManager fragmentManager = getFragmentManager(); 
        FragmentTransaction fragmentTransaction =  
            fragmentManager.beginTransaction(); 
        Fragment1 fragment1 = new Fragment1(); 
        fragmentTransaction.replace(android.R.id.content, fragment1);         
        fragmentTransaction.addToBackStack(null);  
        fragmentTransaction.commit(); 

關於片段就講到這裡了,下一個部分將會講解菜單。很簡單。歡迎大家提出寶貴的意見。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.