Android中Fragment子类及其PreferenceFragment的创建过程演示

来源:互联网 发布:防沉迷软件 编辑:程序博客网 时间:2024/06/13 20:58

Android中Fragment子类及其PreferenceFragment的创建过程演示


这篇文章主要介绍了Android中Fragment子类及其PreferenceFragment的创建过程演示,PreferenceFragment用来保存Fragment的选项设置,需要的朋友可以参考下

Fragment创建方式
Fragment有两种使用方式:静态方式 和 动态方式。
1. 静态方式
第一步:先定义一个Fragment子类。

?
1
2
3
4
5
6
7
8
publicclass ExampleFragment extendsFragment {
 
  @Override
  publicView onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    returninflater.inflate(R.layout.example_fragment, container, false);
  
}

说明:ExampleFragment是Fragment的子类,它的布局定义是example_fragment.xml文件。
第二步:定义Fragment子类对应的布局文件。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal">
 
  <EditTextandroid:id="@+id/edit_message"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:hint="@string/edit_message"/>
 
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage"/>
 
</LinearLayout>

说明:上面是example_fragment.xml的内容。
第三步:在需要用到该Fragment的Activity对应的布局中使用该Fragment。
下面是引用Fragment的Activity的代码:

?
1
2
3
4
5
6
7
8
9
10
publicclass FragmentLayoutTest extendsActivity
{
  /** Called when the activity is first created. */
  @Override
  publicvoid onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }
}

下面是main.xml的内容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/fragment_intro"
    />
 
  <fragmentandroid:name="com.skw.fragmentlayouttest.ExampleFragment"
    android:id="@+id/frag_example"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
 
</LinearLayout>

说明:在该布局文件中通过调用了先前自定义的ExampleFragment。
点击查看:静态方式的完整源码
2. 动态方式
重复"上面的第一步和第二步",实现一个Fragment子类。
第三步:在需要用到该Fragment的Activity对应的布局中使用定义一个FrameLayout。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
 
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/fragment_intro"
    />
 
  <FrameLayout
    android:id="@+id/frag_example"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
 
</LinearLayout>

第四步:在Activity中将Fragment填充到FrameLayout中。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
publicclass FragmentLayoutTest extendsActivity {
 
  @Override
  protectedvoid onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 
    // 获取FragmentManager
    FragmentManager fragmentManager = getFragmentManager();
    // 获取FragmentTransaction   
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
 
    // 获取ExampleFragment
    ExampleFragment fragment = newExampleFragment();
    // 将fragment添加到容器frag_example中
    fragmentTransaction.add(R.id.frag_example, fragment);
    fragmentTransaction.commit();
  }
}

PreferenceFragment使用说明
1. 创建配置文件
新建res/xml/preferences.xml,内容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<PreferenceScreenxmlns:android="http://schemas.android.com/apk/res/android">
 
  <PreferenceCategory
    android:title="PreferenceCategory A">
 
    <!--
     (01) android:key是Preferece的id
     (02) android:title是Preferece的大标题
     (03) android:summary是Preferece的小标题
     -->
    <CheckBoxPreference
      android:key="checkbox_preference"
      android:title="title_checkbox_preference"
      android:summary="summary_checkbox_preference"/>
 
  </PreferenceCategory>
 
  <PreferenceCategory
    android:title="PreferenceCategory B">
 
    <!--
     android:dialogTitle是对话框的标题
     android:defaultValue是默认值
     -->
    <EditTextPreference
      android:key="edittext_preference"
      android:title="title_edittext_preference"
      android:summary="null"
      android:dialogTitle="dialog_title_edittext_preference"
      android:defaultValue="null"/>
 
    <!--
     android:entries是列表中各项的说明
     android:entryValues是列表中各项的值
     -->
    <ListPreference
      android:key="list_preference"
      android:dialogTitle="Choose font"
      android:entries="@array/pref_font_types"
      android:entryValues="@array/pref_font_types_values"
      android:summary="sans"
      android:title="Font"
      android:defaultValue="sans"/>
  </PreferenceCategory>
 
 
  <PreferenceCategory
    android:title="PreferenceCategory C">
 
    <SwitchPreference
      android:key="switch_preferece"
      android:title="title_switch_preferece"
      android:defaultValue="true"/>
 
    <SeekBarPreference
      android:key="seekbar_preference"
      android:title="title_seekbar_preference"
      android:max="100"
      android:defaultValue="30"/>
 
  </PreferenceCategory>
 
</PreferenceScreen>

说明:PreferenceFragment的组件很多,包括CheckBoxPreference, EditTextPreference, ListPreference, SwitchPreference, SeekBarPreference, VolumePreference等。这些组建的属性定义如下。
(01) android:key是Preferece的id,它是Preferece的唯一标识。
(02) android:title是Preferece的大标题。
(03) android:summary是Preferece的小标题。
(04) android:dialogTitle是对话框的标题。
(05) android:defaultValue是默认值。
(06) android:entries是列表中各项的说明。
(07) android:entryValues是列表中各项的值。
注意:SwitchPreference是API 14(Android4.0)才支持的。所以,要想使用SwitchPreference的话,必须在manifest中定义apk支持的最小版本。
<uses-sdk android:minSdkVersion="14" />
2. 自定义PreferenceFragment

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
publicclass PrefsFragment extendsPreferenceFragment
  implementsSharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceClickListener {
  privatestatic final String TAG = "##PrefsFragment##";
 
  privatestatic final String CHECK_PREFERENCE  = "checkbox_preference";
  privatestatic final String EDITTEXT_PREFERENCE = "edittext_preference";
  privatestatic final String LIST_PREFERENCE   = "list_preference";
  privatestatic final String SWITCH_PREFERENCE  = "switch_preferece";
  privatestatic final String SEEKBAR_PREFERENCE = "seekbar_preference";
 
  privatePreference mEditText;
  @Override
  publicvoid onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 
    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);
 
    mEditText = (Preference) findPreference(EDITTEXT_PREFERENCE);
    mEditText.setOnPreferenceClickListener(this);
  }
 
  @Override
  publicvoid onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    // Set summary to be the user-description for the selected value
    Preference connectionPref = findPreference(key);
    if(key.equals(CHECK_PREFERENCE)) {
      booleanchecked = sharedPreferences.getBoolean(key, false);
      Log.d(TAG,"CheckBox: checked="+checked);
    }elseif (key.equals(EDITTEXT_PREFERENCE)) {
      String value = sharedPreferences.getString(key, "");
      connectionPref.setSummary(value);
      Log.d(TAG,"EditText: value="+value);
    }elseif (key.equals(LIST_PREFERENCE)) {
      String value = sharedPreferences.getString(key, "");
      connectionPref.setSummary(value);
      Log.d(TAG,"List: value="+value);
    }elseif (key.equals(SWITCH_PREFERENCE)) {
      booleanchecked = sharedPreferences.getBoolean(key, false);
      Log.d(TAG,"Switch: checked="+checked);
    }elseif (key.equals(SEEKBAR_PREFERENCE)) {
      intvalue = sharedPreferences.getInt(key, 0);
      Log.d(TAG,"Seekbar: value="+value);
    }
  }
 
  @Override
  publicboolean onPreferenceClick(Preference preference) {
    SharedPreferences sharedPreferences = preference.getSharedPreferences();
    String value = sharedPreferences.getString(preference.getKey(), "");
    Log.d(TAG,"onPreferenceClick: value="+value);
 
    returntrue;
  }
 
  @Override
  publicvoid onResume() {
    super.onResume();
    getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
 
  }
 
  @Override
  publicvoid onPause() {
    getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    super.onPause();
  }
}
阅读全文
0 0
原创粉丝点击