androidrepublic_android develop

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

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 使用google earth engine(GEE)提取2000年到2019年长江下游水体(河流、湖泊)数据[通俗易懂]

    使用google earth engine(GEE)提取2000年到2019年长江下游水体(河流、湖泊)数据[通俗易懂]我最近想通过GEE直接统计长时间序列下长江下游流域的水体面积变化情况,如果通过传统做法很复杂,于是想到了使用GEE。下面是提取水体的效果图,时间是2000年的,你也可以设置显示2000-2019年中任意一年的水体数据。代码链接:https://code.earthengine.google.com/2440b9511ba0c1cefaf926c7c47e5ea2读者可以先通过代码看看效果,下面我说一下最主要的思路:1.数据源的选择;2.数据的时间、地点筛选;3.水体的标准设置;4

    2025年12月2日
    5
  • Ubuntu20.04上安装Edge「建议收藏」

    Ubuntu20.04上安装Edge「建议收藏」原文地址:HowtoInstallMicrosoftEdgeBrowseronUbuntu20.04|Linuxizehttps://linuxize.com/post/how-to-install-edge-browser-on-ubuntu-20-04/1.更新源sudoaptupdatesudoaptinstallsoftware-properties-commonapt-transport-httpswget2.导入MicrosoftGPGKey

    2022年7月21日
    17
  • 单片机毕业设计196例「建议收藏」

    单片机毕业设计196例「建议收藏」单片机本科毕业设计——心率计(脉搏测量仪)系统设计与实现(源代码+protues仿真+PCB+开题报告+讲解视频).zip,相关下载链接:https://download.csdn.net/download/dwf1354046363/72630770单片机本科毕业设计——声控灯(继电器)控制系统设计与实现(源代码+protues仿真+PCB+开题报告+讲解视频).zip,相关下载链接:https://download.csdn.net/download/dwf1354046363/72620013单片

    2022年10月4日
    7
  • redis数据类型详解(redis数据类型及常用方法)

    文章目录string类型操作实践incr/incrbydecr/decrbyincrbyfloatappendstrlenmset/mget小节面试分析Key有效时间设计Expire(设置生效时长-单位秒)Persist(取消时长设置)pexpire(单位毫秒)小节面试分析Hash类型应用实践问题hset/hgethincrbyhmset/hmgethexistshdelhkeys/hvals小节面试分析List类型应用实践问题lpushrpush查看listlinsertlsetlremltriml

    2022年4月10日
    58
  • MariaDB安装教程

    MariaDB安装教程注:从MariaDB10.4.3开始,不再提供预构建的数据目录,并且用户需要运行mysql_install_db.exe来创建数据目录1、解压到自定义路径E:\MariaDB\mariadb-10.5.4-winx642、将其中的bin加入path系统环境路径3、用管理员方式打开cmd,并打开bin目录4、输入mysql_install_db.exe–datadir=E:\MariaDB\mariadb-10.5.4-winx64\data–service=MyMar..

    2022年6月14日
    31
  • hi3516dv300芯片手册_hi3518ev300

    hi3516dv300芯片手册_hi3518ev300基于Hi3516DV300的嵌入式入门演练(上)基于Hi3516DV300的嵌入式入门演练(下)文章目录信息5常见外设操作5.1USB无线网卡5.1.1在内核中开启驱动支持5.1.2准备驱动需使用到的固件文件5.1.3使用wpa_supplicant连接到热点5.1.4使用hostapd将网卡作为AP5.2TF卡的挂载5.2.1手动挂载5.2.2使用mdev自动挂载设备5.2.3使用udev自动挂载设备6扩展演练6.1使用Buildroot构建根文件系统6.2理解设备树6.3

    2022年9月24日
    4

发表回复

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

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