編輯:Android開發教程
有的時候,我們做的程序需要提供一些選項的功能,能讓用戶去定制化他們自己的使用風格。舉個例子, 你可能允許用戶是否自動保存登錄信息,允許用戶自己設定某個頁面的刷新時間等等。在Android平台上面, 我們可以使用PreferenceActivity基類去顯示給用戶一個選項設置的界面。在Android3.0或更高的版本上, 可以使用PreferenceFragment類去實現這個功能。
下面將展示如何去創建和使用PreferenceFragment 。
1. 創建一個工程:PreferenceFragmentExample。
2. 在res文件夾下面新建一個xml文件夾 ,在xml文件夾下面新建一個文件:preferences.xml。
<?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。
public class Fragment1 extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 從xml文件加載選項
addPreferencesFromResource(R.xml.preferences);
}
}
4. PreferenceFragmentExampleActivity.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();
}
}
Android開發入門(八)使用活動欄 8.2 添加ActionBar物件
除了在ActionBar的左側顯示應用圖標和Activity的標題,也可以在ActionBar上面顯示額外的項目。這些 被添加的項目被叫做“action it
Android ApiDemos示例解析(16) App->Activity->Translucent
Activity分類示例的最後幾個例子是來顯示半透明Activity。例子大同小異。實現Activity的半透明效果主要是通過Style和 Theme來實現的。看看Tra
面向大眾的移動技術:在Android中為手勢編碼
構建移動應用程序和構建 Web 應用程序一樣,要時刻牢記最終目標(和用戶)。始終了解您的應用程序可 以給用戶帶來哪些利益。您的應用程序將顯示哪些類型的信息,擁有哪些功能
巧用Android網絡通信技術在網絡上直接傳輸對象
要做一個優秀的Android應用,使用到網絡通信技術是必不可少的,很難想象一款沒有網絡交互的軟件最 終能發展得多成功。那麼我們來看一下,一般Android應用程序裡都是怎