首页 > 其他分享 >Fragment初学5——使用Fragment的子类PreferenceFragment

Fragment初学5——使用Fragment的子类PreferenceFragment

时间:2023-09-21 10:08:22浏览次数:32  
标签:xml Fragment 子类 MyPerferenceFragment Preference android public PreferenceFragmen


在Android的应用中通常都有setting功能,能够设置一些全局的选项,例如字体颜色,个人喜好等等。这些东西都存在一个xml中,在android中对应的对象就是SharedPreferences。在android3.0之前,我们一般继承PreferenceActivity这个基类去实现相关的方法。在3.0之后的系统中当然选择使用PreferenceFragment了,原因是PreferenceFragment是一个更加平滑的结构,你可以将它依附在任何的activity上面,这也是谷歌强力推荐的。




常用Preference

CheckPreference —— CheckBox 单选框

EditTextPreference —— EditText 输入文本框

ListPreference —— ListView 列表框

RingtonePreference —— 选择铃声

XML定义常用的属性有:

android:key : 每个Preference控件独一无二的”ID”,唯一表示此Preference。

android:defaultValue : 默认值。 例如,CheckPreference的默认值可为”true”,默认为选中状态;

EditTextPreference的默认值可为”110” 。

android:enabled : 表示该Preference是否可用状态。

android:title : 每个Preference在PreferenceScreen布局上显示的标题——大标题

android:summary : 每个Preference在PreferenceScreen布局上显示的标题——小标题(可以没有)

ListPreference中:

android:entries:类型为array,控件欲显示的文本

android:entryValues:类型为array,与文本相对应的key-value键值对,value保存至sharedPreference文件


说完了,就开始介绍如何使用吧,

1、在项目的res/xml中新建一个preferences.xml。用于定义菜单界面的设置选项


<?xml version="1.0" encoding="utf-8"?>
 
 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
 
 

 
 
    <PreferenceCategory android:title="inline_preferences" >
 
 
        <CheckBoxPreference
 
 
            android:key="checkbox_preference"
 
 
            android:summary="这是一个复选框"
 
 
            android:title="复选框" />
 
 
    </PreferenceCategory>
 
 
    <PreferenceCategory android:title="dialog-based preferences" >
 
 
        <EditTextPreference
 
 
            android:dialogTitle="编辑文本对话框"
 
 
            android:key="edittext_preference"
 
 
            android:summary="这是一个编辑文本对话框"
 
 
            android:title="可编辑文本框" />
 
 

 
 
        <ListPreference
 
 
            android:dialogTitle="请选择一项"
 
 
            android:entries="@array/entries_list_preference"
 
 
            android:entryValues="@array/entryvalues_list_preference"
 
 
            android:key="list_preferenc"
 
 
            android:summary="这是一个列表对话框"
 
 
            android:title="列表对话框" />
 
 
    </PreferenceCategory>
 
 
    <PreferenceCategory android:title="Launch preferences" >
 
 

 
 
        <!-- This PreferenceScreen tag serves as a screen break (similar to page break in word processing). Like for other preference types, we assign a key here so it is able to save and restore its instance state. -->
 
 
        <PreferenceScreen
 
 
            android:key="screen_preference"
 
 
            android:summary="展示另一个首选项配置页面"
 
 
            android:title="页面首选项" >
 
 

 
 
            <!-- 你可以在这里放置更多的首选项内容,将被在下一个页面呈现出来 -->
 
 
            <CheckBoxPreference
 
 
                android:key="next_screen_checkbox_preference"
 
 
                android:summary="在另一个页面展示但出于同一个层级的首选项配置"
 
 
                android:title="复选框设置" />
 
 
        </PreferenceScreen>
 
 
        <PreferenceScreen
 
 
            android:summary="从一个意图中启动一个activity"
 
 
            android:title="意图首选项" >
 
 
            <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="这是一个可见的父类"
 
 
            android:title="父类复选框首选项" />
 
 
        <!-- 子类的可见类型是由样式属性定义的 -->
 
 
        <CheckBoxPreference
 
 
            android:dependency="parent_checkbox_preference"
 
 
            android:key="child_checkbox_preference"
 
 
            android:layout="?android:attr/preferenceLayoutChild"
 
 
            android:summary="这是一个可见的子类"
 
 
            android:title="子类复选框首选项" />
 
 
    </PreferenceCategory>
 
 

 
 
</PreferenceScreen>
 

 
2、创建MyPerferenceFragment.java
 

 
public class MyPerferenceFragment extends PreferenceFragment {
 
 

 
 
    @Override
 
 
    public void onCreate(Bundle savedInstanceState) {
 
 
        // TODO Auto-generated method stub
 
 
        super.onCreate(savedInstanceState);
 
 
        addPreferencesFromResource(R.xml.preferences); 
 
 
    }
 
 

 
 
}
 

 
3、修改MainActivity.java类
 
public class MainActivity extends Activity {
 
 

 
 
    @Override
 
 
    protected void onCreate(Bundle savedInstanceState) {
 
 
        super.onCreate(savedInstanceState);
 
 
        setContentView(R.layout.activity_main);
 
 

 
 
    }
 
 

 
 
    @Override
 
 
    public boolean onCreateOptionsMenu(Menu menu) {
 
 
        // Inflate the menu; this adds items to the action bar if it is present.
 
 
        getMenuInflater().inflate(R.menu.main, menu);
 
 
        return true;
 
 
    }
 
 

 
 
    @Override
 
 
    public boolean onOptionsItemSelected(MenuItem item) {
 
 
        if (item.getItemId() == R.id.action_settings) {
 
 
            FragmentManager fm = getFragmentManager();
 
 
            FragmentTransaction transaction = fm.beginTransaction();
 
 
            MyPerferenceFragment fragment = new MyPerferenceFragment();
 
 
            transaction.replace(R.id.lin, fragment);
 
 
            transaction.commit();
 
 
        }
 
 
        return super.onOptionsItemSelected(item);
 
 
    }
 
 
}


源代码

参考:

http://www.linuxidc.com/Linux/2013-09/90299.htm

标签:xml,Fragment,子类,MyPerferenceFragment,Preference,android,public,PreferenceFragmen
From: https://blog.51cto.com/u_6947107/7548529

相关文章

  • Fragment初学7——Fragment在Android开发中的应用1
    Fragment的常用功能通过getFragmentManager()方法,可以得到FragmentManager对象,主要完成下面的功能(1).得到已经存在Fragment对象如果该fragment在布局文件中指定了id,通过findFragmentById()得到对象,或者指定了tag可以通过findFragmentByTag()得到对象Fragment fragment = getFra......
  • Fragment初学4——使用Fragment的子类ListFragment
    ListFragment的例子,MainActivity左边显示一个ListFragment,右边是一个FrameLayout容器,该FrameLayout容器将会动态更新其中的FragmentMainActivity布局文件如下:<?xmlversion="1.0"encoding="utf-8"?><!--定义一个水平排列的LinearLayout,并指定使用中等分隔条-->......
  • Fragment初学8——Fragment在Android开发中的应用2
    Fragment都是依附于Activity的,通信方式大致也分为如下几种:如果Activity中包含自己管理的Fragment的引用,可以通过直接引用访问所有的Fragment的public方法 如果Activity中未保存任何Fragment的引用,那么可以通过getFragmentManager.findFragmentByTag()或者findFragmentById()获得......
  • 2、protected: 这种权限是为继承而设计的,protected所修饰的成员,对所有子类是可访问的,但
    2、protected:这种权限是为继承而设计的,protected所修饰的成员,对所有子类是可访问的,但只对同包的类是可访问的,对外包的非子类是不可以访问; protected权限的访问范围是:当前包所有的类+当前包以外的子类。 ......
  • 9个视图子类,视图类,视图集,ViewSetMixin, drf之路由
    1.9个视图子类fromrest_framework.genericsimportListAPIView,CreateAPIView,ListCreateAPIViewfromrest_framework.genericsimportRetrieveAPIView,DestroyAPIView,UpdateAPIViewfromrest_framework.genericsimportRetrieveUpdateDestroyAPIView,RetrieveDes......
  • 9个视图子类、视图集、drf之路由
    9个视图子类fromrest_framework.genericsimportListAPIView,CreateAPIView,ListCreateAPIViewfromrest_framework.genericsimportRetrieveAPIView,DestroyAPIView,UpdateAPIViewfromrest_framework.genericsimportRetrieveUpdateDestroyAPIView,RetrieveDestroy......
  • 9个视图子类
    视图类五层第一层:继承APIview,五个接口第二层:GenericAPIView,继承APIview(两个类属性+defgetpostgetputdelete)第三层:五个视图扩展类GenericAPIView+(RetrieveModelMixin,CreateModelMixin,DestroyModelMixin,ListModelMixin,UpdateModelMixin)(两个类属性+defgetpostget......
  • Android入门教程 | DialogFragment 的使用
    弹窗,是常见的一种提示方式。DialogFragment是在3.0时引入的,是一种特殊的Fragment,用于在Activity上展示一个模态的对话框。DialogFragment示例确定UI样式首先我们得知道做成什么样。一般来说简单的弹窗是一个标题,一端文字内容。或者带有一两个按钮。这里我们做一个有标题和文字......
  • 多个fragment切换,而不重新加载数据的实现
     多个fragment切换,而不重新加载数据的实现1、在xml中添加一个framlayout<FrameLayoutandroid:id="@+id/framelayout"android:layout_width="match_parent"android:layout_weight="1"android:layout_height="0dp"......
  • 通过父类创建子类
    通过父类创建子类 usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceZZX.Model.ViewModel{///<summary>///用户所申请的工作///</summary>publicclassUserJob:J......