Activity的使用

本文介绍了LauncherActivity的应用场景及其实现方式,通过一个示例展示了如何使用LauncherActivity创建一个列表,并在点击列表项时启动不同的Activity。同时,文章还提供了两个具体的Activity实现案例,即设置参数界面和星际兵种展示界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面会主要介绍三种Activity的用法:
LauncherActivity的作用在于形成一个列表,点击列表项时,可产生相应的intent进入到其他的Activity界面。
既然是列表,我们当然需要一个适配器来设置列表项的内容,由于LauncherActivity的一个列表项和一个class相关联,所以才能达到点击一个列表项就启动一个Activity的功能。因此还需要一个class数组,用来放要启动的Activity。最后要重写intentForPosition()方法。
具体代码如下:
public class MainActivity extends LauncherActivity {

    //定义列表项显示的内容,最好为两个Activity的名称
    String[] names = {"设置程序参数" , "查看星际兵种"};

    //定义两个Activity对应的实现类
    Class<?>[] clazzs ={PreferenceActivityTest.class , ExpandableListActivityTest.class};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this , R.layout.support_simple_spinner_dropdown_item, names);
        //设置该窗口列表所需的adapter
        setListAdapter(adapter);

    }

    //根据列表项来返回指定Acticity对应的intent
    @Override
    public Intent intentForPosition(int position){
        return new Intent(MainActivity.this , clazzs[position]);
    }
}

上面用到的两个Activity
ExpandableListActivityTest:
ExpandableListActivity的子类,用于显示一个可展开的列表窗口

PerferenceActivity的子类,用于显示一个设置选项参数并保存的窗口
res\xml\preferences.xml
<?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="选择铃声(测试RingtonePreference)"
        android:showDefault="true"
        android:key="ring_key"
        android:showSilent="true"></RingtonePreference>

    <PreferenceCategory android:title="个人信息设置组">
        <EditTextPreference
            android:key="name"
            android:title="填写用户名"
            android:summary="填写您的用户名(测试EditTextPreference)"
            android:dialogTitle="您所使用的用户名为:"/>

        <ListPreference
            android:key="gender"
            android:title="性别"
            android:summary="选择性别(测试ListPreference)"
            android:dialogTitle="ListPreference"
            android:entries="@array/gender_name_list"
            android:entryValues="@array/gender_name_value"/>
    </PreferenceCategory>

    <PreferenceCategory android:title="系统功能设置组">
        <CheckBoxPreference
            android:key="autoSave"
            android:title="自动保存进度"
            android:summaryOn="开启"
            android:summaryOff="关闭"
            android:defaultValue="true"/>
    </PreferenceCategory>

</PreferenceScreen>
PerferenceActivityTest.java
public class PreferenceActivityTest extends PreferenceActivity{
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}




ExpandableListActivityTest.java
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[][]{
                    {"狂战士","龙骑士","黑暗圣堂"},
                    {"小狗","刺蛇","飞龙","自爆飞机"},
                    {"机枪兵","护士","幽灵"}
            };
<span style="white-space:pre">	</span>    //获取指定组位置,指定子列表项数据
            @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.MATCH_PARENT,64);
                TextView textView=new TextView(ExpandableListActivityTest.this);
                textView.setLayoutParams(lp);
                textView.setPadding(36,0,0,0);
                textView.setTextSize(20);
                return textView;
            }
<span style="white-space:pre">	</span>    //该方法决定每个子选项的外观
            @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;
            }

<span style="white-space:pre">	</span>    //获取指定组位置处的组数据
            @Override
            public Object getGroup(int groupPosition) {
                return armTypes[groupPosition];
            }


            @Override
            public int getGroupCount() {
                return armTypes.length;
            }

            @Override
            public long getGroupId(int groupPosition) {
                return groupPosition;
            }
<span style="white-space:pre">	</span>    //该方法决定每个组选项的外观
            @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;
            }
        };
<span style="white-space:pre">	</span>//设置该窗口显示列表
        setListAdapter(adapter);

    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值