讲解:

PreferenceActivity结合PreferenceFragment实现参数设置界面:此界面是专门用来做设置界面的,例如:Android的下拉菜单中的设置就是用的此方法设计的,外形美观,设计简便,无需什么监听器,intent打开之类的,与传统activity设计不同。

PreferenceActivity:负责加载选项设置列表的布局文件 (在这里面设计列表

PreferenceFragment:负责加载选项设置的布局文件 (在这里面设计关于列表内容的设置

编程步骤:

1. 1.1在主activity的.java文件中设置主列表:关于开发启动activity列表请看下一篇帖,1.2对应设计主activity的.xml文件

2.

2.1创建preferenceactivity的.布局文件,其中preferenceactivity中只负责列表的内容其中包括哪个列表参数开启哪个activity

2.2创建preferencefragment的.xml文件,(preferenceactivity中每个参数都要 有一个preferencefragment的.xm l文件:因为点击每个选项都会弹出一个UI)

2.3创建preferenceactivity的.java文件,此文件主要是调用preferenceactivity的.xml文件,另外关于preferencefragment的类也可以放在此文件中(preferenceactivity中每个参数都要有一个preferencefragment的类)

2.4如果主activity列表中还有其他类型的activity 例如(ExpandableListActivity)则还需要增加其他的.xml和.java

2.5注意在AndroidManifest.xml中配置activity (因为在Androidmanifest中主activity是系统设置的,preferencefragment是在preferenceactivity的.xml文件中指定的,所以只需要自己手动配置点击主activity列表中的选项时弹出的界面就好了,有几个选项就要设置几个activity)


下面以一个程序为例详细的说明上面的步骤

1.1在主activity的.java文件中设置主列表 (主列表中有两个内容:一个是preferenceactivity的另一个是ExpandableListActivity)



/**
* 此文件是程序入口文件,这是一个LauncherActivity的继承类,证明此文件的每个列表文件都对应
* 一个Intent,单击列表时出现下一个activity
*/
public class MainActivity extends LauncherActivity
{
//定义两个Activity的名称
String[] names = {"设置程序参数" , "查看星际兵种"};
//定义两个Activity对应的实现类
//列表中就两个内容,单击列表项目时分别调用哪个类用的,一个是preference另一个是expandblelist
Class<?>[] clazzs = {PreferenceActivityTest.class
, ExpandableListActivityTest.class};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1 , names);
// 设置该窗口显示的列表所需的Adapter
setListAdapter(adapter);
}
//根据列表项来返回指定Activity对应的Intent
@Override public Intent intentForPosition(int position)
{
return new Intent(MainActivity.this , clazzs[position]);
}
}


1.2对应设计主activity的.xml文件

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >


<ListView android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000ff"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>


</RelativeLayout>

2.1创建preferenceactivity的.布局文件,此布局文件中包括三个内容分别是:程序的选项设置、界面选项设置、使用intent:并定义了每个选项被点击后弹出的界面

效果如下


<!-- 此文件是preference加载选项设置列表的布局文件
而且定义了当触碰列表中选项时需要弹出的哪个界面的累-->
<preference-headers
xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 指定启动指定PreferenceFragment的列表项 -->
<header android:fragment=
"com.example.preferenceactivitytest.PreferenceActivityTest$Prefs1Fragment"
android:icon="@drawable/ic_settings_applications"
android:title="程序选项设置"
android:summary="设置应用的相关选项" />
<!-- 指定启动指定PreferenceFragment的列表项 -->
<header android:fragment=
"com.example.preferenceactivitytest.PreferenceActivityTest$Prefs2Fragment"
android:icon="@drawable/ic_settings_display"
android:title="界面选项设置 "
android:summary="设置显示界面的相关选项">
<!-- 使用extra可向Activity传入额外的数据 -->
<extra android:name="website"
android:value="www.crazyit.org" />
</header>
<!-- 使用Intent启动指定Activity的列表项 这个主要是与上面的两个preferenceFragment作比较的
这里显示了个网页 -->
<header
android:icon="@drawable/ic_settings_display"
android:title="使用Intent"
android:summary="使用Intent启动某个Activity">
<intent android:action="android.intent.action.VIEW"
android:data="http://www.crazyit.org" />
</header>


</preference-headers>


2.2创建preferencefragment的.xml文件,preferenceactivity中有两个选项是用到preferencefragment的,所以需要创建两个preferencefragment的.xml文件

第一个效果如下


<!-- 此文件是设置preference设置的 -->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 设置系统铃声 -->
<RingtonePreference
android:ringtoneType="all"
android:title="设置铃声"
android:summary="选择铃声(测试RingtonePreference)"
android:showDefault="true"
android:key="ring_key"
android:showSilent="true">
</RingtonePreference>
<PreferenceCategory android:title="个人信息设置组">
<!-- 通过输入框填写用户名 -->
<EditTextPreference
android:key="name"
android:title="填写用户名"
android:summary="填写您的用户名(测试EditTextPreference)"
android:dialogTitle="您所使用的用户名为:" />
<!-- 通过列表框选择性别 -->
<ListPreference
android:key="gender"
android:title="性别"
android:summary="选择您的性别(测试ListPreference)"
android:dialogTitle="ListPreference"
android:entries="@array/gender_name_list"
android:entryValues="@array/gender_value_list" />
</PreferenceCategory>
<PreferenceCategory android:title="系统功能设置组 ">
<CheckBoxPreference
android:key="autoSave"
android:title="自动保存进度"
android:summaryOn="自动保存: 开启"
android:summaryOff="自动保存: 关闭"
android:defaultValue="true" />
</PreferenceCategory>
</PreferenceScreen>

第二个效果如下


<!-- 此文件是用来设置prefernece参数的 -->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="背景灯光组">
<!-- 通过列表框选择灯光强度 -->
<ListPreference
android:key="light"
android:title="灯光强度"
android:summary="请选择灯光强度(测试ListPreference)"
android:dialogTitle="请选择灯光强度"
android:entries="@array/light_strength_list"
android:entryValues="@array/light_value_list" />
</PreferenceCategory>
<PreferenceCategory android:title="文字显示组 ">
<!-- 通过SwitchPreference设置是否自动滚屏 -->
<SwitchPreference
android:key="autoScroll"
android:title="自动滚屏"
android:summaryOn="自动滚屏: 开启"
android:summaryOff="自动滚屏: 关闭"
android:defaultValue="true" />
</PreferenceCategory>
</PreferenceScreen>

2.3创建preferenceactivity的.java文件,此文件主要是调用preferenceactivity的.xml文件,另外关于preferencefragment的类也放在此文件中(preferenceactivity中每个参 数都要 有一个preferencefragment的类)

/**
* 此文件是preferenceactivity列表文件,继承的preferenceactivity类
* 用来对主activity中第一个参数的设置
* 此文件中包含preferenceactivity和preferencefragment共同作用实现参数列表
* 所以包含了preferenceactivity类用来加载显示列表和每个选项对应的一个preferencefragment类用来加载选项设置
*/
public class PreferenceActivityTest extends PreferenceActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// 该方法用于为该界面设置一个标题按钮
if (hasHeaders())
{
Button button = new Button(this);
button.setText("设置操作");
// 将该按钮添加到该界面上
setListFooter(button);
}
}
// 重写该该方法,负责加载页面布局文件
//用于显示preferenceactivity列表的
@Override
public void onBuildHeaders(List<Header> target)
{
// 加载选项设置列表的布局文件
loadHeadersFromResource(R.xml.preference_activity, target);
}
//此类是preferencefragment用于对preferenceactivity列表中第一个选项的设置
public static class Prefs1Fragment extends PreferenceFragment
{

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_fragment1);
}
}
//此类是preferenceragment用于对preferenceactivity列表中第二个选项的设置
public static class Prefs2Fragment extends PreferenceFragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_fragment2);
// 获取传入该Fragment的参数
String website = getArguments().getString("website");
Toast.makeText(getActivity()
, "网站域名是:" + website , Toast.LENGTH_LONG).show();
}
}
}

2.4如果主activity列表中还有其他类型的activity 例如(ExpandableListActivity)则还需要增加其他的.xml和.java

效果如下


无.xml .java如下

/**
* 此文件是activity主界面中的第二个参数被点击时调用的此文件显示的此类调用的界面
*/
public class ExpandableListActivityTest extends ExpandableListActivity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ExpandableListAdapter adapter = new BaseExpandableListAdapter()
{
int[] logos = new int[]
{
R.drawable.p,
R.drawable.z,
R.drawable.t
};
private String[] armTypes = new String[]
{ "神族兵种", "虫族兵种", "人族兵种"};
private String[][] arms = new String[][]
{
{ "狂战士", "龙骑士", "黑暗圣堂", "电兵" },
{ "小狗", "刺蛇", "飞龙", "自爆飞机" },
{ "机枪兵", "护士MM" , "幽灵" }
};
//获取指定组位置、指定子列表项处的子列表项数据
@Override
public Object getChild(int groupPosition, int childPosition)
{
return arms[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition)
{
return arms[groupPosition].length;
}
private TextView getTextView()
{
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textView = new TextView(ExpandableListActivityTest.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
return textView;
}
//该方法决定每个子选项的外观
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
//获取指定组位置处的组数据
@Override
public Object getGroup(int groupPosition)
{
return armTypes[groupPosition];
}
@Override
public int getGroupCount()
{
return armTypes.length;
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
//该方法决定每个组选项的外观
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
LinearLayout ll = new LinearLayout(ExpandableListActivityTest.this);
ll.setOrientation(0);
ImageView logo = new ImageView(ExpandableListActivityTest.this);
logo.setImageResource(logos[groupPosition]);
ll.addView(logo);
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
@Override
public boolean hasStableIds()
{
return true;
}
};
// 设置该窗口显示列表
setListAdapter(adapter);
}
}


2.5在AndroidManifest.xml中配置activity

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.preferenceactivitytest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.preferenceactivitytest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 定义两个Activity -->
<activity
android:name=".ExpandableListActivityTest"
android:label="查看星际兵种">
</activity>
<activity
android:name=".PreferenceActivityTest"
android:label="设置程序参数">
</activity>
</application>
</manifest>




更多相关文章

  1. 新手在路上! Android(安卓)UI界面设计
  2. Android(安卓)系统中camera中 自动对焦的解决方案
  3. AdFreeDetector 开源了
  4. Android通过AIDL实现接听电话、挂断电话操作 | 拨打电话
  5. android第一天学习基本配置与安装过程和启动
  6. 六、ANDROID资源文件
  7. Android(安卓)DiskLruCache完全解析,硬盘缓存的最佳方案
  8. 新浪微博SDK在Eclipse引入
  9. Android自定义背景的设置方法

随机推荐

  1. [置顶] 我的Android进阶之旅------>Andro
  2. Android 国际化工具 Android Localizer
  3. 【Android】Android 博客园客户端 (七)登录
  4. android——json解析
  5. HTC Kaiser 编译Android Kernel zImage
  6. android中ProgressDialog与ProgressBar的
  7. Android 换肤
  8. Scala&Android下的开发trait经验分享
  9. unity安卓多个sdk合并
  10. Android(安卓)4.2 BT系统之蓝牙关闭过程