文章目录
PreferenceFragment是什么
public abstract class PreferenceFragment extends Fragment
以一个列表来展示首选项对象的层级关系,这些首选项将自动地保存为SharedPreferences,使用户可以用他们来进行交互。为了能够重新获得ShaedPreferences的实例,该Fragement中的层级首选项将会在同一个包下面使用带有一个上下文的PreferenceManager.getDefaultSharedPreferences作为这个fragement 。
此外,所展示的首选项将会遵循系统首选项的视觉风格,通过使用XML文件来创建各个首选项的视图层级(可以被显示在许多页面)会非常简单。基于上述原因,推荐在应用中使用这个fragement(作为一个超类)来处理首选项问题。
一个PreferenceScreen对象应该在首选项层级的顶部。此外,随后在层次结构PreferenceScreen表示一个屏幕分割处——就是包含随后的PreferenceScreen应显示在另一个屏幕页面上。首选项框架处理从首选项层次结构显示了这些其他屏幕内容。
首选项层次结构可以有很多种方式形成:
●从一个XML文件制定的层次结构。
●从不同的activity,每一个activity通过meta-data在一个XML文件中制定他自己的首选项。
●从一个以PreferenceScreen为根的层次结构对象。
为了从一个XML文件中获取界面,使用addPreferenceFromResource(int)方法。根元素应该使用PreferenceScreen。随后的元素可以指向实际的首选项的子类。正如上面提到的,在层次结构中随后的PreferenceScreen将导致屏幕分割处。
为了指定一个意图来查询都带有各自首选项的activitiy,使用addPreferenceFromIntent方法。每个activity可以在manifest文件中指定meta-data来指向一个XML文件资源。这些资源文件将被填充到单独的首选项层次结构并且通过这个fragment来展示。
为了指定一个以PreferenceScreen为根元素的对象,使用setPreferenceScreen(PreferenceScreen)方法。
方便起见,这个fragment实现了一个用于当前层次结构中任意首选项的点击事件监听器,onPreferenceTreeClick(PreferenceScreen,Preference).
以上翻译自PreferenceFragment的官方文档,可自行查阅其原版说明
Preferences页面
好了我们来开始我们的项目了首先我们写一个preferences.xml的文件
如果想要详细了解Preference可以参考这一篇文章
android preference 中entries和entryValues
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="In-line preferences">
<CheckBoxPreference
android:key="checkbox_preference"
android:summary="@string/s1"
android:title="@string/s2" />
</PreferenceCategory>
<PreferenceCategory android:title="Dialog-based preferences">
<EditTextPreference
android:dialogTitle="@string/d1"
android:key="edittext_preference"
android:summary="@string/d2"
android:title="@string/d3" />
<!--android:entries设置的内容是我们能够看到的内容,而android:entryValues是实际保存的值。-->
<ListPreference
android:dialogTitle="@string/f1"
android:entries="@array/entries_list_preference"
android:entryValues="@array/entryvalues_list_preference"
android:key="list_preferenc"
android:summary="@string/f2"
android:title="@string/f3" />
</PreferenceCategory>
<PreferenceCategory android:title="Launch preferences">
<PreferenceScreen
android:key="screen_preference"
android:summary="@string/g1"
android:title="@string/g2">
<!-- 你可以在这里放置更多的首选项内容,将被在下一个页面呈现出来 -->
<CheckBoxPreference
android:key="next_screen_checkbox_preference"
android:summary="@string/g3"
android:title="@string/g4" />
</PreferenceScreen>
<PreferenceScreen
android:summary="@string/h1"
android:title="@string/h2">
<intent
android:action="android.intent.action.VIEW"
android:data="http://www.baidu.com" />
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title="Preference attributes">
<CheckBoxPreference
android:key="parent_checkbox_preference"
android:summary="@string/j1"
android:title="@string/j2" />
<!-- 子类的可见类型是由样式属性定义的 -->
<CheckBoxPreference
android:dependency="parent_checkbox_preference"
android:key="child_checkbox_preference"
android:layout="?android:attr/preferenceLayoutChild"
android:summary="@string/k1"
android:title="@string/k2" />
</PreferenceCategory>
</PreferenceScreen>
string页面
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.beta.perferencefragmenttest.MainActivity">
<Button
android:id="@+id/per"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="从这里跳转到设置PreferenceFragment"/>
</android.support.constraint.ConstraintLayout>
> 我们来写UI界面
activity_main.xml这个界面很简单的就一个BUTTON用于简单的跳转
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.beta.perferencefragmenttest.MainActivity">
<Button
android:id="@+id/per"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="从这里跳转到设置PreferenceFragment"/>
</android.support.constraint.ConstraintLayout>
接着我们来写一个MainActivity页面
功能就不分析了就只有一个button用于跳转
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button bt_per = (Button)findViewById(R.id.per);
bt_per.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent(MainActivity.this,FragmentPreferences.class);
startActivity(intent1);
}
});
}
}
最后我们来写FragmentPerfences页面
import android.app.Activity;
import android.os.Bundle;
import android.preference.PreferenceFragment;
public class FragmentPreferences extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//从FragmentManager管理其中得到并且开始传输创建一个新的PerfencesFragment
getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragement()).commit();
}
//这个PrefsFragment继承PreferenceFragment
/*然后我们把我们之前写好的preferences文件给它加载进去那么我们就成功了获取到设置页面的东西了。*/
public static class PrefsFragement extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
}
PS:如果你跳转失败那么就是你在AndroidManifest中没有注册你的FragmentpFerences.class
本文参考自Android学习笔记PreferenceFragment的使用
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/134161.html