2014年7月21日凌晨笔记(转载)

标题:用LauncherActivity开发,启动Activity列表



注:与普通Activity的listView的区别是,单击列表项时,执行的结果是intent,即跳转到另一个对应的Activity中。


一、LauncherActivity的使用

LauncherActivity本质上是一个开发列表界面的Acticity,与普通的列表界面不同的是,它开发出来的列表界面的每个列表项对应于一个Intent,因此当用户点击不同的列表项时,应用程序会启动对应的Activity,需要注意的是继承LauncherActivity时通常应该重写intentForPosition(int position)方法,该方法根据不同的Item返回不同的Intent,从而程序自动启动不同的Activity。

OtherActivity继承LauncherActivity不需要界面布局文件。但通过SimpleAdapter来设置每个Item时需要用到布局文件list.xml:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal"  
  6.      >  
  7.        
  8.          <!-- 定义一个ImageView作为列表项的一部分 -->  
  9.     <ImageView   
  10.         android:id="@+id/header"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_width="wrap_content"  
  13.         android:paddingLeft="10dp"     
  14.         />  
  15.     <TextView   
  16.         android:id="@+id/name"  
  17.         android:layout_height="match_parent"  
  18.         android:layout_width="match_parent"  
  19.         android:textSize="16dp"  
  20.         android:gravity="center_vertical"  
  21.         android:paddingLeft="10dp"  
  22.         />  
  23.       
  24. </LinearLayout>  
<LinearLayout 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:orientation="horizontal"
     >
     
         <!-- 定义一个ImageView作为列表项的一部分 -->
	<ImageView 
		android:id="@+id/header"
		android:layout_height="wrap_content"
		android:layout_width="wrap_content"
		android:paddingLeft="10dp"   
	    />
	<TextView 
	    android:id="@+id/name"
	    android:layout_height="match_parent"
	    android:layout_width="match_parent"
	    android:textSize="16dp"
	    android:gravity="center_vertical"
	    android:paddingLeft="10dp"
	    />
    
</LinearLayout>


OtherActivity.java代码:

  1. package com.example.otheractivity;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. public class OtherActivity extends LauncherActivity {  
  6.       
  7.     //定义两个Activity的名称   
  8.     int []imageIds=new int[]{  
  9.             R.drawable.ic_launcher,  
  10.             R.drawable.ic_launcher  
  11.     };  
  12.     String[] ActivityNames={"设置程序参数","查看星际兵种"};  
  13.     //定义两个Activity对应的实现类   
  14.     Class<?>[] classes={ExpandableActivityTest.class,PreferenceActivityTest.class};  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.           
  19.       //创建一个List集合,List集合的元素是Map     
  20.         List<Map<String, Object>> listItems=new ArrayList<Map<String,Object>>();    
  21.         for(int i=0;i<ActivityNames.length;i++){    
  22.             Map<String, Object> listItem=new HashMap<String, Object>();    
  23.             listItem.put("header", imageIds[i]);    
  24.             listItem.put("personName", ActivityNames[i]);    
  25.             listItems.add(listItem);    
  26.         }   
  27.         SimpleAdapter simpleAdapter=new SimpleAdapter(this, listItems, R.layout.list, new String[]{"personName","header"}, new int[]{R.id.name,R.id.header});  
  28.         //设置该窗口现实的列表所需的Adapter   
  29.         setListAdapter(simpleAdapter);  
  30.     }  
  31.   
  32.     @Override  
  33.     public boolean onCreateOptionsMenu(Menu menu) {  
  34.         getMenuInflater().inflate(R.menu.activity_other, menu);  
  35.         return true;  
  36.     }  
  37.     @Override  
  38.     protected Intent intentForPosition(int position) {  
  39.         // TODO Auto-generated method stub   
  40.           
  41.         return new Intent(OtherActivity.this, classes[position]);  
  42.     }  
  43.       
  44.       
  45. }  

我们先来看下面这张图片:

这张图片显示了Android提供的Activity类。

下面是程序清单:
Ex003_06Activity Java code
  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. }  
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. 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. }  
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. 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. }  
public class PreferenceActivityTest extends PreferenceActivity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		// 设置显示参数设置布局。
		addPreferencesFromResource(R.xml.preferences);
	}
}

preferences.xml code
  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>  
<?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面板即可在里面找到
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值