-
Android开发学习之LauncherActivity开发启动的列表
创建项目:OtherActivity
项目运行结果:
建立主Activity:OtherActivity.java
[java]
package wwj.otherActivity;
import android.os.Bundle;
import android.app.Activity;
import android.app.LauncherActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.support.v4.app.NavUtils;
public class OtherActivity extends LauncherActivity {
//定义两个Activity的名称
String[] names = {"设置程序参数", "查看星际兵种"};
//定义两个Activity对应的实现类
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
protected Intent intentForPosition(int position) {
// TODO Auto-generated method stub
return new Intent(OtherActivity.this, clazzs[position]);
}
}package wwj.otherActivity;
import android.os.Bundle;
import android.app.Activity;
import android.app.LauncherActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.support.v4.app.NavUtils;public class OtherActivity extends LauncherActivity {
//定义两个Activity的名称
String[] names = {"设置程序参数", "查看星际兵种"};
//定义两个Activity对应的实现类
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
protected Intent intentForPosition(int position) {
// TODO Auto-generated method stub
return new Intent(OtherActivity.this, clazzs[position]);
}
}
建立第一个列表项的Activity:PreferenceActivityTest.java[java]
package wwj.otherActivity;
import android.os.Bundle;
public class PreferenceActivityTest extends android.preference.PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//设置显示参数设置布局
addPreferencesFromResource(R.xml.preferences);
}
}package wwj.otherActivity;
import android.os.Bundle;
public class PreferenceActivityTest extends android.preference.PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//设置显示参数设置布局
addPreferencesFromResource(R.xml.preferences);
}
}PreferenceActivity使用的界面布局文件
[html]
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 设置系统铃声 -->
<RingtonePreference
android:ringtoneType="all"
android:title="设置铃声"
android:summary="选择铃声"
android:showDefault="true"
android:key="ring_key"
android:showSilent="true">
</RingtonePreference>
<PreferenceCategory
android:title="个人信息设置组">
<!-- 通过输入框填写用户名 -->
<EditTextPreference
android:key="name"
android:title="用户名"
android:summary="填写你的用户名"
android:dialogTitle="您所使用的用户名为: "
/>
<!-- 通过列表框选择性别 -->
<ListPreference
android:key="gender"
android:title="性别"
android:summary="选择您的性别"
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><?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 设置系统铃声 -->
<RingtonePreference
android:ringtoneType="all"
android:title="设置铃声"
android:summary="选择铃声"
android:showDefault="true"
android:key="ring_key"
android:showSilent="true">
</RingtonePreference>
<PreferenceCategory
android:title="个人信息设置组">
<!-- 通过输入框填写用户名 -->
<EditTextPreference
android:key="name"
android:title="用户名"
android:summary="填写你的用户名"
android:dialogTitle="您所使用的用户名为: "
/>
<!-- 通过列表框选择性别 -->
<ListPreference
android:key="gender"
android:title="性别"
android:summary="选择您的性别"
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>
数组文件:array.xml[html]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="gender_name_list">
<item>男</item>
<item>女</item>
</string-array>
<string-array name="gender_value_list">
<item>male</item>
<item>female</item>
</string-array>
</resources><?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="gender_name_list">
<item>男</item>
<item>女</item>
</string-array>
<string-array name="gender_value_list">
<item>male</item>
<item>female</item>
</string-array>
</resources>创建第二个列表项的Activity:ExpandableListActivity.java
[java]
package wwj.otherActivity;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ExpandableListActivityTest extends ExpandableListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
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", "幽灵"}
};
//获取指定组位置、指定子列表项处的子列表项数据
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return arms[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;};
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
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;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
//获取指定位置处的组数据
public Object getGroup(int groupPosition) {
return armTypes[groupPosition];
};
public int getGroupCount() {
// TODO Auto-generated method stub
return armTypes.length;
}
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
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;
}
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
public boolean isChildSelectable(int groupPosition,
int childPosition) {
// TODO Auto-generated method stub
return true;
}
};
//设置该窗口显示列表
setListAdapter(adapter);
}
}