Android中Fragment子類及其PreferenceFragment的創(chuàng)建過程演示
Fragment創(chuàng)建方式
Fragment有兩種使用方式:靜態(tài)方式 和 動(dòng)態(tài)方式。
1. 靜態(tài)方式
第一步:先定義一個(gè)Fragment子類。
public class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
說明:ExampleFragment是Fragment的子類,它的布局定義是example_fragment.xml文件。
第二步:定義Fragment子類對(duì)應(yīng)的布局文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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" >
<EditText android: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的內(nèi)容。
第三步:在需要用到該Fragment的Activity對(duì)應(yīng)的布局中使用該Fragment。
下面是引用Fragment的Activity的代碼:
public class FragmentLayoutTest extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
下面是main.xml的內(nèi)容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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"
/>
<fragment android: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>
說明:在該布局文件中通過調(diào)用了先前自定義的ExampleFragment。
點(diǎn)擊查看:靜態(tài)方式的完整源碼
2. 動(dòng)態(tài)方式
重復(fù)"上面的第一步和第二步",實(shí)現(xiàn)一個(gè)Fragment子類。
第三步:在需要用到該Fragment的Activity對(duì)應(yīng)的布局中使用定義一個(gè)FrameLayout。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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中。
public class FragmentLayoutTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 獲取FragmentManager
FragmentManager fragmentManager = getFragmentManager();
// 獲取FragmentTransaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// 獲取ExampleFragment
ExampleFragment fragment = new ExampleFragment();
// 將fragment添加到容器frag_example中
fragmentTransaction.add(R.id.frag_example, fragment);
fragmentTransaction.commit();
}
}
PreferenceFragment使用說明
1. 創(chuàng)建配置文件
新建res/xml/preferences.xml,內(nèi)容如下:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="PreferenceCategory A">
<!--
(01) android:key是Preferece的id
(02) android:title是Preferece的大標(biāo)題
(03) android:summary是Preferece的小標(biāo)題
-->
<CheckBoxPreference
android:key="checkbox_preference"
android:title="title_checkbox_preference"
android:summary="summary_checkbox_preference" />
</PreferenceCategory>
<PreferenceCategory
android:title="PreferenceCategory B">
<!--
android:dialogTitle是對(duì)話框的標(biāo)題
android:defaultValue是默認(rèn)值
-->
<EditTextPreference
android:key="edittext_preference"
android:title="title_edittext_preference"
android:summary="null"
android:dialogTitle="dialog_title_edittext_preference"
android:defaultValue="null" />
<!--
android:entries是列表中各項(xiàng)的說明
android:entryValues是列表中各項(xiàng)的值
-->
<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的唯一標(biāo)識(shí)。
(02) android:title是Preferece的大標(biāo)題。
(03) android:summary是Preferece的小標(biāo)題。
(04) android:dialogTitle是對(duì)話框的標(biāo)題。
(05) android:defaultValue是默認(rèn)值。
(06) android:entries是列表中各項(xiàng)的說明。
(07) android:entryValues是列表中各項(xiàng)的值。
注意:SwitchPreference是API 14(Android4.0)才支持的。所以,要想使用SwitchPreference的話,必須在manifest中定義apk支持的最小版本。
<uses-sdk android:minSdkVersion="14" />
2. 自定義PreferenceFragment
public class PrefsFragment extends PreferenceFragment
implements SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceClickListener {
private static final String TAG = "##PrefsFragment##";
private static final String CHECK_PREFERENCE = "checkbox_preference";
private static final String EDITTEXT_PREFERENCE = "edittext_preference";
private static final String LIST_PREFERENCE = "list_preference";
private static final String SWITCH_PREFERENCE = "switch_preferece";
private static final String SEEKBAR_PREFERENCE = "seekbar_preference";
private Preference mEditText;
@Override
public void 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
public void 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)) {
boolean checked = sharedPreferences.getBoolean(key, false);
Log.d(TAG, "CheckBox: checked="+checked);
} else if (key.equals(EDITTEXT_PREFERENCE)) {
String value = sharedPreferences.getString(key, "");
connectionPref.setSummary(value);
Log.d(TAG, "EditText: value="+value);
} else if (key.equals(LIST_PREFERENCE)) {
String value = sharedPreferences.getString(key, "");
connectionPref.setSummary(value);
Log.d(TAG, "List: value="+value);
} else if (key.equals(SWITCH_PREFERENCE)) {
boolean checked = sharedPreferences.getBoolean(key, false);
Log.d(TAG, "Switch: checked="+checked);
} else if (key.equals(SEEKBAR_PREFERENCE)) {
int value = sharedPreferences.getInt(key, 0);
Log.d(TAG, "Seekbar: value="+value);
}
}
@Override
public boolean onPreferenceClick(Preference preference) {
SharedPreferences sharedPreferences = preference.getSharedPreferences();
String value = sharedPreferences.getString(preference.getKey(), "");
Log.d(TAG, "onPreferenceClick: value="+value);
return true;
}
@Override
public void onResume() {
super.onResume();
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
}
}
說明:PreferenceFragment中的每一項(xiàng)都是一個(gè)SharedPreferences對(duì)象,它們會(huì)像SharedPreferences存儲(chǔ)在該APK的私有數(shù)據(jù)區(qū)。監(jiān)聽PreferenceFragment中的成員有多種方式,常用的兩種就是:
(01) 監(jiān)聽數(shù)據(jù)的變化:通過實(shí)現(xiàn)SharedPreferences.OnSharedPreferenceChangeListener接口,來監(jiān)聽PreferenceFragment中每一項(xiàng)的數(shù)據(jù)變化。
(02) 監(jiān)聽點(diǎn)擊事件:通過實(shí)現(xiàn)Preference.OnPreferenceClickListener接口,來監(jiān)聽PreferenceFragment中每一項(xiàng)的點(diǎn)擊動(dòng)作。
3. 使用PreferenceFragment
前面已經(jīng)定義好了一個(gè)PreferenceFragment。接下來,就可以實(shí)例化它的對(duì)象,并將其在Activity中進(jìn)行顯示。
public class FragmentTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new PrefsFragment()).commit();
}
}
相關(guān)文章
android計(jì)算器實(shí)現(xiàn)兩位數(shù)的加減乘除
這篇文章主要為大家詳細(xì)介紹了android計(jì)算器實(shí)現(xiàn)兩位數(shù)的加減乘除,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
Android Studio 3.6 正式版終于發(fā)布了,快來圍觀
Android Studio 3.6 正式版終于發(fā)布了,值得興奮呀,畢竟 3.5 大版本更新也已經(jīng)差不多半年了,撒花撒花!這次更新又更新了什么呢?快來跟隨小編一起看看吧2020-02-02
Android N 7.0中報(bào)錯(cuò):android.os.FileUriExposedException的解決方法
這篇文章主要給大家介紹了關(guān)于在Android N 7.0中報(bào)錯(cuò):android.os.FileUriExposedException的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧2018-05-05
Android設(shè)置TextView樣式SpannableString教程
這篇文章主要為大家介紹了Android設(shè)置TextView樣式SpannableString教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android中實(shí)現(xiàn)水平滑動(dòng)(橫向滑動(dòng))ListView示例
這篇文章主要介紹了Android中實(shí)現(xiàn)水平滑動(dòng)(橫向滑動(dòng))ListView示例,本文用自己封裝一個(gè)控件的方法解決了這個(gè)需求,需要的朋友可以參考下2015-06-06
Android TextView實(shí)現(xiàn)圖文混合編排的方法
這篇文章主要為大家詳細(xì)介紹了Android TextView實(shí)現(xiàn)圖文混合編排的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android RecyclerView詳解之實(shí)現(xiàn) ListView GridView瀑布流效果
RecyclerView 是Android L版本中新添加的一個(gè)用來取代ListView的SDK,它的靈活性與可替代性比listview更好2016-07-07

