Android开发学习之LauncherActivity开发启动的列表

来源:互联网 发布:巨人网络ceo 编辑:程序博客网 时间:2024/05/18 02:00

Android开发学习之LauncherActivity开发启动的列表

创建项目:OtherActivity

项目运行结果:

 

 

建立主Activity:OtherActivity.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]);    }}


建立第一个列表项的Activity:PreferenceActivityTest.java

 

package wwj.otherActivity;import android.os.Bundle;public class PreferenceActivityTest extends android.preference.PreferenceActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);//设置显示参数设置布局addPreferencesFromResource(R.xml.preferences);}}


PreferenceActivity使用的界面布局文件

<?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

<?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

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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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 stubreturn arms[groupPosition][childPosition];}public long getChildId(int groupPosition, int childPosition) {return childPosition;};public int getChildrenCount(int groupPosition) {// TODO Auto-generated method stubreturn 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 stubTextView 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 stubreturn armTypes.length;}public long getGroupId(int groupPosition) {// TODO Auto-generated method stubreturn groupPosition;}public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {// TODO Auto-generated method stubLinearLayout 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 stubreturn true;}public boolean isChildSelectable(int groupPosition,int childPosition) {// TODO Auto-generated method stubreturn true;}};//设置该窗口显示列表setListAdapter(adapter);}}