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


相关推荐

  • css如何设置超链接样式

    css如何设置超链接样式css设置超链接样式是通过伪类来实现的 (1):link:设置a对象在未被访问前的样式表属性。(2):visited:设置a对象在其链接地址已被访问过时的样式表属性。(3):hover:设置对象在其鼠标悬停时的样式表属性。(4):active:设置对象在被用户激活(在鼠标点击与释放之间发生的事件)时的样式表属性。  css中关于超链接的四个属性正确顺序为:

    2022年7月19日
    18
  • java写爬虫没python好吗_java爬虫代码

    java写爬虫没python好吗_java爬虫代码写在前面前段时间实习结束回来休息,某日闲逛无意间又打开了半次元,突然看见几个好漂亮的coser小姐姐,就想存个图片当壁纸(づ ̄3 ̄)づ╭❤~然而又发现,很多小姐姐都设置了作品禁止保存或者是右下角带水印身为一个重度强迫症患者,默默打开浏览器,看看能不能偷鸡扒到原图。。。开始之前1.所有图片都是各位作者付出辛苦劳动得来的,请尊重coser版权。2.图片自己下载使用可以,请勿用于商业用途,转载请先取…

    2022年9月14日
    0
  • 计算机错误代码0x 00000006,什么原因造成了蓝屏 电脑蓝屏错误代码介绍

    计算机错误代码0x 00000006,什么原因造成了蓝屏 电脑蓝屏错误代码介绍电脑蓝屏是在上网的时候再常见到的现象了,造成电脑蓝屏的原因很多,所以微软在操作系统中设计了蓝屏代码,让大家电脑在出现蓝屏的时候能够及时的发现是什么原因造成了蓝屏。一般蓝屏代码都位于屏幕提示文字的第一段或者倒数第三段,但是蓝屏代码都是以“***Stop”开头。编号代码含意00×00000000作业完成。10×00000001不正确的函数。20×00000002系统找不到指定的档案。…

    2022年5月15日
    142
  • 这7个web前端开发写代码软件,你过用几个?[通俗易懂]

    群里的朋友,经常问到web前端开发写代码用那个软件好?今天在这里统一回答下,主流的web前端开发写代码的软件有这些Webstorm、Vscode、SublimeText、HBuilder、Dreamweaver、notepad++、editplus等,做前端这么多年了,下面谈下我的使用感受吧。1.WebStorm【推荐】WebStorm是jetbra…

    2022年4月11日
    75
  • sp_executesql返回多个值

    sp_executesql返回多个值declare@sqlnvarchar(2000)declare@ccintdeclare@ssintselect@sql=Nselect@c=(selectcount(*)fromyg);select@s=(selectsum(b_id)fromyg)EXECUTEsp_executesql@sql,N@cintout,@sinto

    2022年5月18日
    34
  • [python]解析通达信盘后数据获取历史日线数据[通俗易懂]

    [python]解析通达信盘后数据获取历史日线数据[通俗易懂]转自:http://bbs.pinggu.org/forum.php?mod=viewthread&tid=4804415&page=1平时我们在做离线的模型回溯测试时候,需要历史的k线数据。可是通达信的日线数据如下:日线数据在通达信的安装目录:vipdoc\sh\lday下面本地的通达信是没有开放api和外部的自己的交易回溯测试…

    2022年7月24日
    105

发表回复

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

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