用LauncherActivity开发启动Activity列表

我们先来看下面这张图片:
这张图片显示了Android提供的Activity类。

下面是程序清单:
Ex003_06Activity Java code
  1. publicclassExpandableListActivityTestextendsExpandableListActivity{
  2. publicvoidonCreate(BundlesavedInstanceState){
  3. super.onCreate(savedInstanceState);
  4. ExpandableListAdapteradapter=newBaseExpandableListAdapter(){
  5. int[]logos=newint[]{R.drawable.p,R.drawable.z,R.drawable.t};
  6. privateString[]armTypes=newString[]{"神族兵种","虫族兵种","人族兵种"};
  7. privateString[][]arms=newString[][]{
  8. {"狂战士","龙骑士","黑暗圣堂","电兵"},
  9. {"小狗","刺蛇","飞龙","自爆飞机"},{"机枪兵","护士MM","幽灵"}};
  10. //获取指定组位置、指定子列表项处的子列表项数据
  11. @Override
  12. publicObjectgetChild(intgroupPosition,intchildPosition){
  13. returnarms[groupPosition][childPosition];
  14. }
  15. @Override
  16. publiclonggetChildId(intgroupPosition,intchildPosition){
  17. returnchildPosition;
  18. }
  19. @Override
  20. publicintgetChildrenCount(intgroupPosition){
  21. returnarms[groupPosition].length;
  22. }
  23. privateTextViewgetTextView(){
  24. AbsListView.LayoutParamslp=newAbsListView.LayoutParams(
  25. ViewGroup.LayoutParams.FILL_PARENT,64);
  26. TextViewtextView=newTextView(
  27. ExpandableListActivityTest.this);
  28. textView.setLayoutParams(lp);
  29. textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
  30. textView.setPadding(36,0,0,0);
  31. textView.setTextSize(20);
  32. returntextView;
  33. }
  34. //该方法决定每个子选项的外观
  35. @Override
  36. publicViewgetChildView(intgroupPosition,intchildPosition,
  37. booleanisLastChild,ViewconvertView,ViewGroupparent){
  38. TextViewtextView=getTextView();
  39. textView.setText(getChild(groupPosition,childPosition)
  40. .toString());
  41. returntextView;
  42. }
  43. //获取指定组位置处的组数据
  44. @Override
  45. publicObjectgetGroup(intgroupPosition){
  46. returnarmTypes[groupPosition];
  47. }
  48. @Override
  49. publicintgetGroupCount(){
  50. returnarmTypes.length;
  51. }
  52. @Override
  53. publiclonggetGroupId(intgroupPosition){
  54. returngroupPosition;
  55. }
  56. //该方法决定每个组选项的外观
  57. @Override
  58. publicViewgetGroupView(intgroupPosition,booleanisExpanded,
  59. ViewconvertView,ViewGroupparent){
  60. LinearLayoutll=newLinearLayout(
  61. ExpandableListActivityTest.this);
  62. ll.setOrientation(0);
  63. ImageViewlogo=newImageView(ExpandableListActivityTest.this);
  64. logo.setImageResource(logos[groupPosition]);
  65. ll.addView(logo);
  66. TextViewtextView=getTextView();
  67. textView.setText(getGroup(groupPosition).toString());
  68. ll.addView(textView);
  69. returnll;
  70. }
  71. @Override
  72. publicbooleanisChildSelectable(intgroupPosition,
  73. intchildPosition){
  74. returntrue;
  75. }
  76. @Override
  77. publicbooleanhasStableIds(){
  78. returntrue;
  79. }
  80. };
  81. //设置该窗口显示列表
  82. setListAdapter(adapter);
  83. }
  84. }
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.FILL_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);
	}
}

ExpandableListActivityTest Java code
  1. publicclassExpandableListActivityTestextendsExpandableListActivity{
  2. publicvoidonCreate(BundlesavedInstanceState){
  3. super.onCreate(savedInstanceState);
  4. ExpandableListAdapteradapter=newBaseExpandableListAdapter(){
  5. int[]logos=newint[]{R.drawable.p,R.drawable.z,R.drawable.t};
  6. privateString[]armTypes=newString[]{"神族兵种","虫族兵种","人族兵种"};
  7. privateString[][]arms=newString[][]{
  8. {"狂战士","龙骑士","黑暗圣堂","电兵"},
  9. {"小狗","刺蛇","飞龙","自爆飞机"},{"机枪兵","护士MM","幽灵"}};
  10. //获取指定组位置、指定子列表项处的子列表项数据
  11. @Override
  12. publicObjectgetChild(intgroupPosition,intchildPosition){
  13. returnarms[groupPosition][childPosition];
  14. }
  15. @Override
  16. publiclonggetChildId(intgroupPosition,intchildPosition){
  17. returnchildPosition;
  18. }
  19. @Override
  20. publicintgetChildrenCount(intgroupPosition){
  21. returnarms[groupPosition].length;
  22. }
  23. privateTextViewgetTextView(){
  24. AbsListView.LayoutParamslp=newAbsListView.LayoutParams(
  25. ViewGroup.LayoutParams.FILL_PARENT,64);
  26. TextViewtextView=newTextView(
  27. ExpandableListActivityTest.this);
  28. textView.setLayoutParams(lp);
  29. textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
  30. textView.setPadding(36,0,0,0);
  31. textView.setTextSize(20);
  32. returntextView;
  33. }
  34. //该方法决定每个子选项的外观
  35. @Override
  36. publicViewgetChildView(intgroupPosition,intchildPosition,
  37. booleanisLastChild,ViewconvertView,ViewGroupparent){
  38. TextViewtextView=getTextView();
  39. textView.setText(getChild(groupPosition,childPosition)
  40. .toString());
  41. returntextView;
  42. }
  43. //获取指定组位置处的组数据
  44. @Override
  45. publicObjectgetGroup(intgroupPosition){
  46. returnarmTypes[groupPosition];
  47. }
  48. @Override
  49. publicintgetGroupCount(){
  50. returnarmTypes.length;
  51. }
  52. @Override
  53. publiclonggetGroupId(intgroupPosition){
  54. returngroupPosition;
  55. }
  56. //该方法决定每个组选项的外观
  57. @Override
  58. publicViewgetGroupView(intgroupPosition,booleanisExpanded,
  59. ViewconvertView,ViewGroupparent){
  60. LinearLayoutll=newLinearLayout(
  61. ExpandableListActivityTest.this);
  62. ll.setOrientation(0);
  63. ImageViewlogo=newImageView(ExpandableListActivityTest.this);
  64. logo.setImageResource(logos[groupPosition]);
  65. ll.addView(logo);
  66. TextViewtextView=getTextView();
  67. textView.setText(getGroup(groupPosition).toString());
  68. ll.addView(textView);
  69. returnll;
  70. }
  71. @Override
  72. publicbooleanisChildSelectable(intgroupPosition,
  73. intchildPosition){
  74. returntrue;
  75. }
  76. @Override
  77. publicbooleanhasStableIds(){
  78. returntrue;
  79. }
  80. };
  81. //设置该窗口显示列表
  82. setListAdapter(adapter);
  83. }
  84. }
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.FILL_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);
	}
}
PreferenceActivityTest Java code
  1. publicclassPreferenceActivityTestextendsPreferenceActivity
  2. {
  3. @Override
  4. publicvoidonCreate(BundlesavedInstanceState)
  5. {
  6. super.onCreate(savedInstanceState);
  7. //设置显示参数设置布局。
  8. addPreferencesFromResource(R.xml.preferences);
  9. }
  10. }
public class PreferenceActivityTest extends PreferenceActivity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		// 设置显示参数设置布局。
		addPreferencesFromResource(R.xml.preferences);
	}
}

preferences.xml code
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <PreferenceScreenxmlns:android="http://schemas.android.com/apk/res/android">
  3. <!--设置系统铃声-->
  4. <RingtonePreference
  5. android:key="ring_key"
  6. android:ringtoneType="all"
  7. android:showDefault="true"
  8. android:showSilent="true"
  9. android:summary="选择铃声(测试RingtonePreference)"
  10. android:title="设置铃声">
  11. </RingtonePreference>
  12. <PreferenceCategoryandroid:title="个人信息设置zu">
  13. <!--通过输入框填写用户名-->
  14. <EditTextPreference
  15. android:dialogTitle="您所使用的用户名为:"
  16. android:key="name"
  17. android:summary="填写您的用户名(测试EditTextPreference)"
  18. android:title="填写用户名"/>
  19. <!--通过列表框选择性别-->
  20. <ListPreference
  21. android:dialogTitle="ListPreference"
  22. android:entries="@array/gender_name_list"
  23. android:entryValues="@array/gender_value_list"
  24. android:key="gender"
  25. android:summary="选择您的性别(测试ListPreference)"
  26. android:title="性别"/>
  27. </PreferenceCategory>
  28. <PreferenceCategoryandroid:title="系统功能设置组">
  29. <CheckBoxPreference
  30. android:defaultValue="true"
  31. android:key="autoSave"
  32. android:summaryOff="自动保存:关闭"
  33. android:summaryOn="自动保存:开启"
  34. android:title="自动保存进度"/>
  35. </PreferenceCategory>
  36. </PreferenceScreen>
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 设置系统铃声 -->
    <RingtonePreference
        android:key="ring_key"
        android:ringtoneType="all"
        android:showDefault="true"
        android:showSilent="true"
        android:summary="选择铃声(测试RingtonePreference)"
        android:title="设置铃声" >
    </RingtonePreference>

    <PreferenceCategory android:title="个人信息设置zu" >

        <!-- 通过输入框填写用户名 -->
        <EditTextPreference
            android:dialogTitle="您所使用的用户名为:"
            android:key="name"
            android:summary="填写您的用户名(测试EditTextPreference)"
            android:title="填写用户名" />
        <!-- 通过列表框选择性别 -->
        <ListPreference
            android:dialogTitle="ListPreference"
            android:entries="@array/gender_name_list"
            android:entryValues="@array/gender_value_list"
            android:key="gender"
            android:summary="选择您的性别(测试ListPreference)"
            android:title="性别" />
    </PreferenceCategory>
    <PreferenceCategory android:title="系统功能设置组 " >
        <CheckBoxPreference
            android:defaultValue="true"
            android:key="autoSave"
            android:summaryOff="自动保存: 关闭"
            android:summaryOn="自动保存: 开启"
            android:title="自动保存进度" />
    </PreferenceCategory>

</PreferenceScreen>
preferences.xml定义了一个参数设置界面
我们还需要在AndroidManifest.xml文件中设置下:
  1. <!--定义两个Activity-->
  2. <activity
  3. android:name=".ExpandableListActivityTest"
  4. android:label="查看星际兵种">
  5. </activity>
  6. <activity
  7. android:name=".PreferenceActivityTest"
  8. android:label="设置程序参数">
  9. </activity>
 <!-- 定义两个Activity -->
        <activity
            android:name=".ExpandableListActivityTest"
            android:label="查看星际兵种" >
        </activity>
        <activity
            android:name=".PreferenceActivityTest"
            android:label="设置程序参数" >
        </activity>


下面我们来看下程序运行后的结果:

点击设置参数后的界面是:

点击查看星际兵种的界面是:

本程序运行后将会在data/data/com.android.Ex003_06/shared_prefs/路径下生成一个/com.android.Ex003_06_preferences.xml文件。打开DDMS的File Explorer面板即可在里面找到。
我们先来看下面这张图片:
这张图片显示了Android提供的Activity类。

下面是程序清单:
Ex003_06Activity Java code
  1. publicclassExpandableListActivityTestextendsExpandableListActivity{
  2. publicvoidonCreate(BundlesavedInstanceState){
  3. super.onCreate(savedInstanceState);
  4. ExpandableListAdapteradapter=newBaseExpandableListAdapter(){
  5. int[]logos=newint[]{R.drawable.p,R.drawable.z,R.drawable.t};
  6. privateString[]armTypes=newString[]{"神族兵种","虫族兵种","人族兵种"};
  7. privateString[][]arms=newString[][]{
  8. {"狂战士","龙骑士","黑暗圣堂","电兵"},
  9. {"小狗","刺蛇","飞龙","自爆飞机"},{"机枪兵","护士MM","幽灵"}};
  10. //获取指定组位置、指定子列表项处的子列表项数据
  11. @Override
  12. publicObjectgetChild(intgroupPosition,intchildPosition){
  13. returnarms[groupPosition][childPosition];
  14. }
  15. @Override
  16. publiclonggetChildId(intgroupPosition,intchildPosition){
  17. returnchildPosition;
  18. }
  19. @Override
  20. publicintgetChildrenCount(intgroupPosition){
  21. returnarms[groupPosition].length;
  22. }
  23. privateTextViewgetTextView(){
  24. AbsListView.LayoutParamslp=newAbsListView.LayoutParams(
  25. ViewGroup.LayoutParams.FILL_PARENT,64);
  26. TextViewtextView=newTextView(
  27. ExpandableListActivityTest.this);
  28. textView.setLayoutParams(lp);
  29. textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
  30. textView.setPadding(36,0,0,0);
  31. textView.setTextSize(20);
  32. returntextView;
  33. }
  34. //该方法决定每个子选项的外观
  35. @Override
  36. publicViewgetChildView(intgroupPosition,intchildPosition,
  37. booleanisLastChild,ViewconvertView,ViewGroupparent){
  38. TextViewtextView=getTextView();
  39. textView.setText(getChild(groupPosition,childPosition)
  40. .toString());
  41. returntextView;
  42. }
  43. //获取指定组位置处的组数据
  44. @Override
  45. publicObjectgetGroup(intgroupPosition){
  46. returnarmTypes[groupPosition];
  47. }
  48. @Override
  49. publicintgetGroupCount(){
  50. returnarmTypes.length;
  51. }
  52. @Override
  53. publiclonggetGroupId(intgroupPosition){
  54. returngroupPosition;
  55. }
  56. //该方法决定每个组选项的外观
  57. @Override
  58. publicViewgetGroupView(intgroupPosition,booleanisExpanded,
  59. ViewconvertView,ViewGroupparent){
  60. LinearLayoutll=newLinearLayout(
  61. ExpandableListActivityTest.this);
  62. ll.setOrientation(0);
  63. ImageViewlogo=newImageView(ExpandableListActivityTest.this);
  64. logo.setImageResource(logos[groupPosition]);
  65. ll.addView(logo);
  66. TextViewtextView=getTextView();
  67. textView.setText(getGroup(groupPosition).toString());
  68. ll.addView(textView);
  69. returnll;
  70. }
  71. @Override
  72. publicbooleanisChildSelectable(intgroupPosition,
  73. intchildPosition){
  74. returntrue;
  75. }
  76. @Override
  77. publicbooleanhasStableIds(){
  78. returntrue;
  79. }
  80. };
  81. //设置该窗口显示列表
  82. setListAdapter(adapter);
  83. }
  84. }
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.FILL_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);
	}
}

ExpandableListActivityTest Java code
  1. publicclassExpandableListActivityTestextendsExpandableListActivity{
  2. publicvoidonCreate(BundlesavedInstanceState){
  3. super.onCreate(savedInstanceState);
  4. ExpandableListAdapteradapter=newBaseExpandableListAdapter(){
  5. int[]logos=newint[]{R.drawable.p,R.drawable.z,R.drawable.t};
  6. privateString[]armTypes=newString[]{"神族兵种","虫族兵种","人族兵种"};
  7. privateString[][]arms=newString[][]{
  8. {"狂战士","龙骑士","黑暗圣堂","电兵"},
  9. {"小狗","刺蛇","飞龙","自爆飞机"},{"机枪兵","护士MM","幽灵"}};
  10. //获取指定组位置、指定子列表项处的子列表项数据
  11. @Override
  12. publicObjectgetChild(intgroupPosition,intchildPosition){
  13. returnarms[groupPosition][childPosition];
  14. }
  15. @Override
  16. publiclonggetChildId(intgroupPosition,intchildPosition){
  17. returnchildPosition;
  18. }
  19. @Override
  20. publicintgetChildrenCount(intgroupPosition){
  21. returnarms[groupPosition].length;
  22. }
  23. privateTextViewgetTextView(){
  24. AbsListView.LayoutParamslp=newAbsListView.LayoutParams(
  25. ViewGroup.LayoutParams.FILL_PARENT,64);
  26. TextViewtextView=newTextView(
  27. ExpandableListActivityTest.this);
  28. textView.setLayoutParams(lp);
  29. textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
  30. textView.setPadding(36,0,0,0);
  31. textView.setTextSize(20);
  32. returntextView;
  33. }
  34. //该方法决定每个子选项的外观
  35. @Override
  36. publicViewgetChildView(intgroupPosition,intchildPosition,
  37. booleanisLastChild,ViewconvertView,ViewGroupparent){
  38. TextViewtextView=getTextView();
  39. textView.setText(getChild(groupPosition,childPosition)
  40. .toString());
  41. returntextView;
  42. }
  43. //获取指定组位置处的组数据
  44. @Override
  45. publicObjectgetGroup(intgroupPosition){
  46. returnarmTypes[groupPosition];
  47. }
  48. @Override
  49. publicintgetGroupCount(){
  50. returnarmTypes.length;
  51. }
  52. @Override
  53. publiclonggetGroupId(intgroupPosition){
  54. returngroupPosition;
  55. }
  56. //该方法决定每个组选项的外观
  57. @Override
  58. publicViewgetGroupView(intgroupPosition,booleanisExpanded,
  59. ViewconvertView,ViewGroupparent){
  60. LinearLayoutll=newLinearLayout(
  61. ExpandableListActivityTest.this);
  62. ll.setOrientation(0);
  63. ImageViewlogo=newImageView(ExpandableListActivityTest.this);
  64. logo.setImageResource(logos[groupPosition]);
  65. ll.addView(logo);
  66. TextViewtextView=getTextView();
  67. textView.setText(getGroup(groupPosition).toString());
  68. ll.addView(textView);
  69. returnll;
  70. }
  71. @Override
  72. publicbooleanisChildSelectable(intgroupPosition,
  73. intchildPosition){
  74. returntrue;
  75. }
  76. @Override
  77. publicbooleanhasStableIds(){
  78. returntrue;
  79. }
  80. };
  81. //设置该窗口显示列表
  82. setListAdapter(adapter);
  83. }
  84. }
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.FILL_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);
	}
}
PreferenceActivityTest Java code
  1. publicclassPreferenceActivityTestextendsPreferenceActivity
  2. {
  3. @Override
  4. publicvoidonCreate(BundlesavedInstanceState)
  5. {
  6. super.onCreate(savedInstanceState);
  7. //设置显示参数设置布局。
  8. addPreferencesFromResource(R.xml.preferences);
  9. }
  10. }
public class PreferenceActivityTest extends PreferenceActivity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		// 设置显示参数设置布局。
		addPreferencesFromResource(R.xml.preferences);
	}
}

preferences.xml code
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <PreferenceScreenxmlns:android="http://schemas.android.com/apk/res/android">
  3. <!--设置系统铃声-->
  4. <RingtonePreference
  5. android:key="ring_key"
  6. android:ringtoneType="all"
  7. android:showDefault="true"
  8. android:showSilent="true"
  9. android:summary="选择铃声(测试RingtonePreference)"
  10. android:title="设置铃声">
  11. </RingtonePreference>
  12. <PreferenceCategoryandroid:title="个人信息设置zu">
  13. <!--通过输入框填写用户名-->
  14. <EditTextPreference
  15. android:dialogTitle="您所使用的用户名为:"
  16. android:key="name"
  17. android:summary="填写您的用户名(测试EditTextPreference)"
  18. android:title="填写用户名"/>
  19. <!--通过列表框选择性别-->
  20. <ListPreference
  21. android:dialogTitle="ListPreference"
  22. android:entries="@array/gender_name_list"
  23. android:entryValues="@array/gender_value_list"
  24. android:key="gender"
  25. android:summary="选择您的性别(测试ListPreference)"
  26. android:title="性别"/>
  27. </PreferenceCategory>
  28. <PreferenceCategoryandroid:title="系统功能设置组">
  29. <CheckBoxPreference
  30. android:defaultValue="true"
  31. android:key="autoSave"
  32. android:summaryOff="自动保存:关闭"
  33. android:summaryOn="自动保存:开启"
  34. android:title="自动保存进度"/>
  35. </PreferenceCategory>
  36. </PreferenceScreen>
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 设置系统铃声 -->
    <RingtonePreference
        android:key="ring_key"
        android:ringtoneType="all"
        android:showDefault="true"
        android:showSilent="true"
        android:summary="选择铃声(测试RingtonePreference)"
        android:title="设置铃声" >
    </RingtonePreference>

    <PreferenceCategory android:title="个人信息设置zu" >

        <!-- 通过输入框填写用户名 -->
        <EditTextPreference
            android:dialogTitle="您所使用的用户名为:"
            android:key="name"
            android:summary="填写您的用户名(测试EditTextPreference)"
            android:title="填写用户名" />
        <!-- 通过列表框选择性别 -->
        <ListPreference
            android:dialogTitle="ListPreference"
            android:entries="@array/gender_name_list"
            android:entryValues="@array/gender_value_list"
            android:key="gender"
            android:summary="选择您的性别(测试ListPreference)"
            android:title="性别" />
    </PreferenceCategory>
    <PreferenceCategory android:title="系统功能设置组 " >
        <CheckBoxPreference
            android:defaultValue="true"
            android:key="autoSave"
            android:summaryOff="自动保存: 关闭"
            android:summaryOn="自动保存: 开启"
            android:title="自动保存进度" />
    </PreferenceCategory>

</PreferenceScreen>
preferences.xml定义了一个参数设置界面
我们还需要在AndroidManifest.xml文件中设置下:
  1. <!--定义两个Activity-->
  2. <activity
  3. android:name=".ExpandableListActivityTest"
  4. android:label="查看星际兵种">
  5. </activity>
  6. <activity
  7. android:name=".PreferenceActivityTest"
  8. android:label="设置程序参数">
  9. </activity>
 <!-- 定义两个Activity -->
        <activity
            android:name=".ExpandableListActivityTest"
            android:label="查看星际兵种" >
        </activity>
        <activity
            android:name=".PreferenceActivityTest"
            android:label="设置程序参数" >
        </activity>


下面我们来看下程序运行后的结果:

点击设置参数后的界面是:

点击查看星际兵种的界面是:

本程序运行后将会在data/data/com.android.Ex003_06/shared_prefs/路径下生成一个/com.android.Ex003_06_preferences.xml文件。打开DDMS的File Explorer面板即可在里面找到。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值