本文主要是介绍Android中LauncherActivity、PreferenceActivity、ExpandableListActivity的综合运用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
随时随地技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
1.LauncherActivity继承了ListActivity,因此它本质上也是一个开发列表界面的Activity,但它开发出来的列表界面与普通列表界面有所不同。它的每个列表项都对应一个Intent,用户单击不同的列表项时,应用程序会自动启动对应的Activity。继承LauncherActivity时通常应该重写intentForPosition()方法,该方法根据不同列表项返回不同的Intent。
2.PreferenceActivity专门用于设置选项参数并进行保存,对于PreferenceActivity,我们不再使用普通的界面布局文件,不再使用XxxLayout作为根元素,而是使用PreferenceScreen作为根元素,它表明定义一个参数设置的界面布局。
3.至于ExpandableListActivity,用于显示一个可展开的列表窗口。
下面通过一个简单实例来演示它们的使用,主界面是LauncherActivity,它的两个列表项是PreferenceActivity和ExpandableListActivity,代码如下:
LauncherActivity:
package com.lovo.activity;import android.os.Bundle;
import android.app.LauncherActivity;
import android.content.Intent;
import android.widget.ArrayAdapter;public class MainActivity extends LauncherActivity {// 定义两个Activity的名称private String[] names = { "查看DNF游戏角色", "设置程序参数" };// 定义两个Activity对应的实现类private Class<?>[] clazzs = { ExpandableListActivityTest.class,PreferenceActivityTest.class };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, names);// 设置该窗口显示的列表所需的AdaptersetListAdapter(adapter);}// 根据列表项返回指定Activity对应的Intent@Overrideprotected Intent intentForPosition(int position) {return new Intent(MainActivity.this, clazzs[position]);}
}
ExpandableListActivity:
package com.lovo.activity;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.LinearLayout;
import android.widget.TextView;public class ExpandableListActivityTest extends ExpandableListActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 创建一个可展开的列表组件ExpandableListAdapter对象ExpandableListAdapter adapter = new BaseExpandableListAdapter() {// 每一个列表的字符数组private String[] roleTypes = new String[] { "圣职者", "神枪手", "鬼剑士","法师", "暗夜使者" };// 每个列表下面的子列表字符数组private String[][] roles = new String[][] {{ "蓝拳", "驱魔", "圣骑士", "复仇者" }, { "漫游", "大枪", "弹药", "机械" },{ "剑魂", "红眼", "阿修罗", "鬼泣" },{ "战斗法师", "魔法师", "元素师", "召唤师" }, { "刺客", "死灵" } };@Overridepublic boolean isChildSelectable(int groupPosition,int childPosition) {return true;}@Overridepublic boolean hasStableIds() {return true;}// 构建每个父列表组件@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {// 创建一个LinearLayout对象LinearLayout ll = new LinearLayout(ExpandableListActivityTest.this);ll.setOrientation(0);TextView textView = getTextView();// 根据groupPosition取roleTypes对应文本作为textView显示的文本内容textView.setText(getGroup(groupPosition).toString());ll.addView(textView);return ll;}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic int getGroupCount() {return roleTypes.length;}@Overridepublic Object getGroup(int groupPosition) {return roleTypes[groupPosition];}@Overridepublic int getChildrenCount(int groupPosition) {return roles[groupPosition].length;}/*** 在MainActivity里创建TextView组件* * @return TextView*/private TextView getTextView() {AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_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;}// 构建子列表的数据@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {TextView textView = getTextView();// 根据groupPosition和childPosition取roles中对应文本作为textView显示的内容textView.setText(getChild(groupPosition, childPosition).toString());return textView;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic Object getChild(int groupPosition, int childPosition) {return roles[groupPosition][childPosition];}};setListAdapter(adapter);}
}
PreferenceActivity:
package com.lovo.activity;import android.os.Bundle;
import android.preference.PreferenceActivity;public class PreferenceActivityTest extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//设置布局addPreferencesFromResource(R.xml.preference);
}
}
PreferenceActivity布局XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" ><!-- 设置系统铃声 --><RingtonePreferenceandroid:key="ring_key"android:ringtoneType="all"android:showDefault="true"android:showSilent="true"android:summary="选择铃声(测试RingtonePreference)"android:title="设置铃声" /><PreferenceCategory android:title="个人信息设置组" ><!-- 通过输入框填写用户名 --><EditTextPreferenceandroid:dialogTitle="您所使用的用户名为:"android:key="name"android:summary="请填写您的用户名(测试EditTextPreference)"android:title="填写用户名" /><!-- 通过列表框选择性别 --><ListPreferenceandroid: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="系统功能设置组" ><CheckBoxPreferenceandroid:defaultValue="true"android:key="autoSave"android:summaryOff="自动保存:关闭"android:summaryOn="自动保存:开启"android:title="自动保存进度" /></PreferenceCategory></PreferenceScreen>
引用的数组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>1</item><item>0</item></string-array></resources>
这篇关于Android中LauncherActivity、PreferenceActivity、ExpandableListActivity的综合运用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!