zoukankan      html  css  js  c++  java
  • Android PreferenceFragment

    PreferenceFragment用来显示首选项的设置,效果图如下:

    主布局文件:

    <RelativeLayout 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"
        tools:context=".MainActivity" >
    
        <fragment
            android:id="@+id/fragment1"
            android:name="com.example.preferencefragment.prefe"//自己的包名和java文件
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
             />
    
    </RelativeLayout>

    新建一个prefe.java类

    package com.example.preferencefragment;
    
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.preference.Preference;
    import android.preference.PreferenceFragment;
    import android.preference.PreferenceManager;
    import android.preference.PreferenceScreen;
    
    public class prefe extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            // TODO 自动生成的方法存根
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
            
        }
    
    }

    在res文件夹下新建xml文件夹,命名为:preferences.xml

    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <PreferenceCategory android:title="In-line 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/cities"
                android:entryValues="@array/cities"
                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>

    MainActivity.java

    package com.example.preferencefragment;
    
    import android.os.Bundle;
    import android.preference.PreferenceFragment;
    import android.preference.PreferenceManager;
    import android.app.Activity;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    import android.content.SharedPreferences;
    import android.view.Menu;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO 自动生成的方法存根
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //加载PrefFragment
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            prefe prefFragment = new prefe();
            transaction.add(R.id.fragment1, prefFragment);
            transaction.commit();
            
            //SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(this);
            //String name=sp.getString("key", "");
        }
    }
  • 相关阅读:
    阿里消息队列中间件 RocketMQ 源码分析 —— Message 拉取与消费(上)
    数据库中间件 ShardingJDBC 源码分析 —— SQL 解析(三)之查询SQL
    数据库分库分表中间件 ShardingJDBC 源码分析 —— SQL 解析(六)之删除SQL
    数据库分库分表中间件 ShardingJDBC 源码分析 —— SQL 解析(五)之更新SQL
    消息队列中间件 RocketMQ 源码分析 —— Message 存储
    源码圈 300 胖友的书单整理
    数据库分库分表中间件 ShardingJDBC 源码分析 —— SQL 路由(一)分库分表配置
    数据库分库分表中间件 ShardingJDBC 源码分析 —— SQL 解析(四)之插入SQL
    数据库分库分表中间件 ShardingJDBC 源码分析 —— SQL 路由(二)之分库分表路由
    C#中Math类的用法
  • 原文地址:https://www.cnblogs.com/hyyweb/p/5249473.html
Copyright © 2011-2022 走看看