用LauncherActivity开发启动Activity列表

我们先来看下面这张图片:
这张图片显示了Android提供的Activity类。
用LauncherActivity开发启动Activity列表_第1张图片
下面是程序清单:
Ex003_06Activity Java code
[java] view plain copy print ?
  1. public class ExpandableListActivityTest extends ExpandableListActivity {  
  2.     public void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.         ExpandableListAdapter adapter = new BaseExpandableListAdapter() {  
  5.             int[] logos = new int[] { R.drawable.p, R.drawable.z, R.drawable.t };  
  6.             private String[] armTypes = new String[] { "神族兵种""虫族兵种""人族兵种" };  
  7.             private String[][] arms = new String[][] {  
  8.                     { "狂战士""龙骑士""黑暗圣堂""电兵" },  
  9.                     { "小狗""刺蛇""飞龙""自爆飞机" }, { "机枪兵""护士MM""幽灵" } };  
  10.   
  11.             // 获取指定组位置、指定子列表项处的子列表项数据  
  12.             @Override  
  13.             public Object getChild(int groupPosition, int childPosition) {  
  14.                 return arms[groupPosition][childPosition];  
  15.             }  
  16.   
  17.             @Override  
  18.             public long getChildId(int groupPosition, int childPosition) {  
  19.                 return childPosition;  
  20.             }  
  21.   
  22.             @Override  
  23.             public int getChildrenCount(int groupPosition) {  
  24.                 return arms[groupPosition].length;  
  25.             }  
  26.   
  27.             private TextView getTextView() {  
  28.                 AbsListView.LayoutParams lp = new AbsListView.LayoutParams(  
  29.                         ViewGroup.LayoutParams.FILL_PARENT, 64);  
  30.                 TextView textView = new TextView(  
  31.                         ExpandableListActivityTest.this);  
  32.                 textView.setLayoutParams(lp);  
  33.                 textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
  34.                 textView.setPadding(36000);  
  35.                 textView.setTextSize(20);  
  36.                 return textView;  
  37.             }  
  38.   
  39.             // 该方法决定每个子选项的外观   
  40.             @Override  
  41.             public View getChildView(int groupPosition, int childPosition,  
  42.                     boolean isLastChild, View convertView, ViewGroup parent) {  
  43.                 TextView textView = getTextView();  
  44.                 textView.setText(getChild(groupPosition, childPosition)  
  45.                         .toString());  
  46.                 return textView;  
  47.             }  
  48.   
  49.             // 获取指定组位置处的组数据   
  50.             @Override  
  51.             public Object getGroup(int groupPosition) {  
  52.                 return armTypes[groupPosition];  
  53.             }  
  54.   
  55.             @Override  
  56.             public int getGroupCount() {  
  57.                 return armTypes.length;  
  58.             }  
  59.   
  60.             @Override  
  61.             public long getGroupId(int groupPosition) {  
  62.                 return groupPosition;  
  63.             }  
  64.   
  65.             // 该方法决定每个组选项的外观   
  66.             @Override  
  67.             public View getGroupView(int groupPosition, boolean isExpanded,  
  68.                     View convertView, ViewGroup parent) {  
  69.                 LinearLayout ll = new LinearLayout(  
  70.                         ExpandableListActivityTest.this);  
  71.                 ll.setOrientation(0);  
  72.                 ImageView logo = new ImageView(ExpandableListActivityTest.this);  
  73.                 logo.setImageResource(logos[groupPosition]);  
  74.                 ll.addView(logo);  
  75.                 TextView textView = getTextView();  
  76.                 textView.setText(getGroup(groupPosition).toString());  
  77.                 ll.addView(textView);  
  78.                 return ll;  
  79.             }  
  80.   
  81.             @Override  
  82.             public boolean isChildSelectable(int groupPosition,  
  83.                     int childPosition) {  
  84.                 return true;  
  85.             }  
  86.   
  87.             @Override  
  88.             public boolean hasStableIds() {  
  89.                 return true;  
  90.             }  
  91.         };  
  92.         // 设置该窗口显示列表   
  93.         setListAdapter(adapter);  
  94.     }  
  95. }  

ExpandableListActivityTest Java code
[java] view plain copy print ?
  1. public class ExpandableListActivityTest extends ExpandableListActivity {  
  2.     public void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.         ExpandableListAdapter adapter = new BaseExpandableListAdapter() {  
  5.             int[] logos = new int[] { R.drawable.p, R.drawable.z, R.drawable.t };  
  6.             private String[] armTypes = new String[] { "神族兵种""虫族兵种""人族兵种" };  
  7.             private String[][] arms = new String[][] {  
  8.                     { "狂战士""龙骑士""黑暗圣堂""电兵" },  
  9.                     { "小狗""刺蛇""飞龙""自爆飞机" }, { "机枪兵""护士MM""幽灵" } };  
  10.   
  11.             // 获取指定组位置、指定子列表项处的子列表项数据  
  12.             @Override  
  13.             public Object getChild(int groupPosition, int childPosition) {  
  14.                 return arms[groupPosition][childPosition];  
  15.             }  
  16.   
  17.             @Override  
  18.             public long getChildId(int groupPosition, int childPosition) {  
  19.                 return childPosition;  
  20.             }  
  21.   
  22.             @Override  
  23.             public int getChildrenCount(int groupPosition) {  
  24.                 return arms[groupPosition].length;  
  25.             }  
  26.   
  27.             private TextView getTextView() {  
  28.                 AbsListView.LayoutParams lp = new AbsListView.LayoutParams(  
  29.                         ViewGroup.LayoutParams.FILL_PARENT, 64);  
  30.                 TextView textView = new TextView(  
  31.                         ExpandableListActivityTest.this);  
  32.                 textView.setLayoutParams(lp);  
  33.                 textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
  34.                 textView.setPadding(36000);  
  35.                 textView.setTextSize(20);  
  36.                 return textView;  
  37.             }  
  38.   
  39.             // 该方法决定每个子选项的外观   
  40.             @Override  
  41.             public View getChildView(int groupPosition, int childPosition,  
  42.                     boolean isLastChild, View convertView, ViewGroup parent) {  
  43.                 TextView textView = getTextView();  
  44.                 textView.setText(getChild(groupPosition, childPosition)  
  45.                         .toString());  
  46.                 return textView;  
  47.             }  
  48.   
  49.             // 获取指定组位置处的组数据   
  50.             @Override  
  51.             public Object getGroup(int groupPosition) {  
  52.                 return armTypes[groupPosition];  
  53.             }  
  54.   
  55.             @Override  
  56.             public int getGroupCount() {  
  57.                 return armTypes.length;  
  58.             }  
  59.   
  60.             @Override  
  61.             public long getGroupId(int groupPosition) {  
  62.                 return groupPosition;  
  63.             }  
  64.   
  65.             // 该方法决定每个组选项的外观   
  66.             @Override  
  67.             public View getGroupView(int groupPosition, boolean isExpanded,  
  68.                     View convertView, ViewGroup parent) {  
  69.                 LinearLayout ll = new LinearLayout(  
  70.                         ExpandableListActivityTest.this);  
  71.                 ll.setOrientation(0);  
  72.                 ImageView logo = new ImageView(ExpandableListActivityTest.this);  
  73.                 logo.setImageResource(logos[groupPosition]);  
  74.                 ll.addView(logo);  
  75.                 TextView textView = getTextView();  
  76.                 textView.setText(getGroup(groupPosition).toString());  
  77.                 ll.addView(textView);  
  78.                 return ll;  
  79.             }  
  80.   
  81.             @Override  
  82.             public boolean isChildSelectable(int groupPosition,  
  83.                     int childPosition) {  
  84.                 return true;  
  85.             }  
  86.   
  87.             @Override  
  88.             public boolean hasStableIds() {  
  89.                 return true;  
  90.             }  
  91.         };  
  92.         // 设置该窗口显示列表   
  93.         setListAdapter(adapter);  
  94.     }  
  95. }  
PreferenceActivityTest Java code
[java] view plain copy print ?
  1. public class PreferenceActivityTest extends PreferenceActivity  
  2. {  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState)  
  5.     {  
  6.         super.onCreate(savedInstanceState);  
  7.         // 设置显示参数设置布局。   
  8.         addPreferencesFromResource(R.xml.preferences);  
  9.     }  
  10. }  

preferences.xml code
[html] view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <!-- 设置系统铃声 -->  
  5.     <RingtonePreference  
  6.         android:key="ring_key"  
  7.         android:ringtoneType="all"  
  8.         android:showDefault="true"  
  9.         android:showSilent="true"  
  10.         android:summary="选择铃声(测试RingtonePreference)"  
  11.         android:title="设置铃声" >  
  12.     </RingtonePreference>  
  13.   
  14.     <PreferenceCategory android:title="个人信息设置zu" >  
  15.   
  16.         <!-- 通过输入框填写用户名 -->  
  17.         <EditTextPreference  
  18.             android:dialogTitle="您所使用的用户名为:"  
  19.             android:key="name"  
  20.             android:summary="填写您的用户名(测试EditTextPreference)"  
  21.             android:title="填写用户名" />  
  22.         <!-- 通过列表框选择性别 -->  
  23.         <ListPreference  
  24.             android:dialogTitle="ListPreference"  
  25.             android:entries="@array/gender_name_list"  
  26.             android:entryValues="@array/gender_value_list"  
  27.             android:key="gender"  
  28.             android:summary="选择您的性别(测试ListPreference)"  
  29.             android:title="性别" />  
  30.     </PreferenceCategory>  
  31.     <PreferenceCategory android:title="系统功能设置组 " >  
  32.         <CheckBoxPreference  
  33.             android:defaultValue="true"  
  34.             android:key="autoSave"  
  35.             android:summaryOff="自动保存: 关闭"  
  36.             android:summaryOn="自动保存: 开启"  
  37.             android:title="自动保存进度" />  
  38.     </PreferenceCategory>  
  39.   
  40. </PreferenceScreen>  
preferences.xml定义了一个参数设置界面
我们还需要在AndroidManifest.xml文件中设置下:
[java] view plain copy print ?
  1. <!-- 定义两个Activity -->  
  2.        <activity  
  3.            android:name=".ExpandableListActivityTest"  
  4.            android:label="查看星际兵种" >  
  5.        </activity>  
  6.        <activity  
  7.            android:name=".PreferenceActivityTest"  
  8.            android:label="设置程序参数" >  
  9.        </activity>  


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

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

点击查看星际兵种的界面是:
用LauncherActivity开发启动Activity列表_第2张图片

本程序运行后将会在data/data/com.android.Ex003_06/shared_prefs/路径下生成一个/com.android.Ex003_06_preferences.xml文件。打开DDMS的File Explorer面板即可在里面找到。

你可能感兴趣的:(用LauncherActivity开发启动Activity列表)