androidrepublic_android develop

androidrepublic_android developPreferenceFragment用来显示首选项的设置,效果图如下:主布局文件:<RelativeLayoutxmlns:android=”http://schemas.android.com/apk/res/android”xmlns:tools=”http://schemas.android.com/tools”android:…


Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

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

androidrepublic_android develop

 

主布局文件:

<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", "");
    }
}

 

转载于:https://www.cnblogs.com/hyyweb/p/5249473.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/196471.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 阿里云大数据存储密集型实例d2s云服务器配置性能详解

    阿里云大数据存储密集型实例d2s云服务器配置性能详解阿里云大数据存储密集型实例d2s云服务器配置性能CPU、内存、适用场景、大数据存储密集型d2s实例规格族和优惠报价信息,InstanceTypes分享大数据存储密集型d2s实例详解:大数据存储密集型d2s实例规格族特性I/O优化实例支持ESSD云盘、SSD云盘和高效云盘实例配备大容量、高吞吐SATAHDD本地盘,辅以最大35Gbit/s实例间网络带宽支持在线更换坏盘,支持热插拔坏盘,避免导致实例停机处理器:2.5GHz主频的Intel®Xeon®Platinum8163(Sky.

    2022年5月2日
  • vim怎么高亮显示_vim配置教程

    vim怎么高亮显示_vim配置教程经常使用SecureCRT在linux下修改一些配置文件,使用vi打开总是一个颜色,找起来比较头大。因为vi不支持高亮,所以需要使用vim。安装vim:yuminstall-yvim(ubuntu使用apt-get)全局(所有用户)vim配置文件路径:/etc/vimrc当前用户vim配置文件路径:~/.vimrc如果存在该文件,则备份一下,以免以后找不到。在vimrc文件中输入:synta…

    2022年9月29日
  • 联合主键注解「建议收藏」

    联合主键注解「建议收藏」//含有联合主键的映射@Entity@Getter@Setter@Table(name=”sign_table”)@IdClass(InfoLeadersId.class)publicclassInfosLeadersextendsBaseEntityimplementsSerializable{@Id@Column(name=”info_id”)privateLonginfoId;@Id@Column(name=.

    2022年6月26日
  • 文件名太长无法删除怎么办

    文件名太长无法删除怎么办

    2021年10月24日
  • Oracle Sequence Cache 参数说明[通俗易懂]

    Oracle Sequence Cache 参数说明[通俗易懂]之前整理的一篇文章:   ORACLESEQUENCE介绍http://blog.csdn.net/tianlesoftware/archive/2009/10/30/4745039.aspx 之前整理的一篇文章。那是还是写blog初期的作品。2009年10月份的。转眼一年,写Blog也比以前成熟了很多。 一.理论知识先看一个创建Sequence的语句:SQL>creates

    2022年10月9日
  • ubuntu clion 2020 永久激活码_通用破解码

    ubuntu clion 2020 永久激活码_通用破解码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月16日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号