标题:用LauncherActivity开发,启动Activity列表
注:与普通Activity的listView的区别是,单击列表项时,执行的结果是intent,即跳转到另一个对应的Activity中。
一、LauncherActivity的使用
LauncherActivity本质上是一个开发列表界面的Acticity,与普通的列表界面不同的是,它开发出来的列表界面的每个列表项对应于一个Intent,因此当用户点击不同的列表项时,应用程序会启动对应的Activity,需要注意的是继承LauncherActivity时通常应该重写intentForPosition(int position)方法,该方法根据不同的Item返回不同的Intent,从而程序自动启动不同的Activity。
OtherActivity继承LauncherActivity不需要界面布局文件。但通过SimpleAdapter来设置每个Item时需要用到布局文件list.xml:
- <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>
<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代码:
- package com.example.otheractivity;
- import java.util.ArrayList;
- public class OtherActivity extends LauncherActivity {
- //定义两个Activity的名称
- int []imageIds=new int[]{
- R.drawable.ic_launcher,
- R.drawable.ic_launcher
- };
- String[] ActivityNames={"设置程序参数","查看星际兵种"};
- //定义两个Activity对应的实现类
- Class<?>[] classes={ExpandableActivityTest.class,PreferenceActivityTest.class};
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //创建一个List集合,List集合的元素是Map
- List<Map<String, Object>> listItems=new ArrayList<Map<String,Object>>();
- for(int i=0;i<ActivityNames.length;i++){
- Map<String, Object> listItem=new HashMap<String, Object>();
- listItem.put("header", imageIds[i]);
- listItem.put("personName", ActivityNames[i]);
- listItems.add(listItem);
- }
- SimpleAdapter simpleAdapter=new SimpleAdapter(this, listItems, R.layout.list, new String[]{"personName","header"}, new int[]{R.id.name,R.id.header});
- //设置该窗口现实的列表所需的Adapter
- setListAdapter(simpleAdapter);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_other, menu);
- return true;
- }
- @Override
- protected Intent intentForPosition(int position) {
- // TODO Auto-generated method stub
- return new Intent(OtherActivity.this, classes[position]);
- }
- }
我们先来看下面这张图片:
这张图片显示了Android提供的Activity类。
下面是程序清单:
Ex003_06Activity Java code
- 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);
- }
- }
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
- 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);
- }
- }
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
- public class PreferenceActivityTest extends PreferenceActivity
- {
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- // 设置显示参数设置布局。
- addPreferencesFromResource(R.xml.preferences);
- }
- }
public class PreferenceActivityTest extends PreferenceActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// 设置显示参数设置布局。
addPreferencesFromResource(R.xml.preferences);
}
}
preferences.xml code
- <?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>
<?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文件中设置下:
- <!-- 定义两个Activity -->
- <activity
- android:name=".ExpandableListActivityTest"
- android:label="查看星际兵种" >
- </activity>
- <activity
- android:name=".PreferenceActivityTest"
- android:label="设置程序参数" >
- </activity>
<!-- 定义两个Activity -->
<activity
android:name=".ExpandableListActivityTest"
android:label="查看星际兵种" >
</activity>
<activity
android:name=".PreferenceActivityTest"
android:label="设置程序参数" >
</activity>
下面我们来看下程序运行后的结果:
点击设置参数后的界面是:
点击查看星际兵种的界面是: