Android PreferenceFragment 的使用

有的时候,我们做的程序需要提供一些选项的功能,能让用户去定制化他们自己的使用风格。举个例子,你可能允许用户是否自动保存登录信息,允许用户自己设定某个页面的刷新时间等等。在Android平台上面,我们可以使用PreferenceActivity基类去显示给用户一个选项设置的界面。在Android3.0或更高的版本上,可以使用PreferenceFragment类去实现这个功能。

下面将展示如何去创建和使用PreferenceFragment。

1、创建一个工程:PreferenceFragmentExample。

2、在res文件夹下面新建一个xml文件夹,在xml文件夹下面新建一个文件:preferences.xml。

 

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

    <PreferenceCategory android:title="Category 1">
        <CheckBoxPreference
            android:title="Checkbox"
            android:defaultValue="false"
            android:summary="True of False"
            android:key="checkboxPref" />
        </PreferenceCategory>                
        
    <PreferenceCategory android:title="Category 2">
        <EditTextPreference
            android:name="EditText"
            android:summary="Enter a string"
            android:defaultValue="[Enter a string here]"
            android:title="Edit Text"
            android:key="editTextPref" />            
        <RingtonePreference
            android:name="Ringtone Preference"
            android:summary="Select a ringtone"
            android:title="Ringtones"
            android:key="ringtonePref" />            
        <PreferenceScreen                
            android:title="Second Preference Screen"
            android:summary=
                "Click here to go to the second Preference Screen"
            android:key="secondPrefScreenPref">                            
            <EditTextPreference
                android:name="EditText"
                android:summary="Enter a string"
                android:title="Edit Text (second Screen)"
                android:key="secondEditTextPref" />                
        </PreferenceScreen>        
    </PreferenceCategory>  
          
</PreferenceScreen>
View Code

 

3、在包路径下面新建一个类:Fragment1.java。

package com.example.preferencefragmentexample;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.preference.PreferenceFragment;

@SuppressLint("NewApi")
public class Fragment1 extends PreferenceFragment {
    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 从xml文件加载选项
        addPreferencesFromResource(R.xml.preferences);
    }
}
View Code

 

4、PreferenceFragmentExampleActivity.java(主活动)的代码。

package com.example.preferencefragmentexample;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = 
            fragmentManager.beginTransaction();
        Fragment1 fragment1 = new Fragment1();
        fragmentTransaction.replace(android.R.id.content, fragment1);        
        fragmentTransaction.addToBackStack(null); 
        fragmentTransaction.commit();
    }
}
View Code

 

 

 

转载于:https://www.cnblogs.com/MMLoveMeMM/articles/3337480.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值