Activity

==>

android中四大组件:Activity、Service、BroadcastReceiver、ContentProvider

Activity组件用于对用户呈现操作界面,不同的Activity呈现的UI不同,添加新的Activity需要在AndroidMainfest.xml添加对应的配置——否则新添加的activity将无法正常使用。

android应用要求所有应用程序组件(Activity、Service、BroadcastReceiver、ContentProvider)都必须显式进行配置。

配置Activity通常需要指定如下三个属性:

  1.name:指定Activity的实现类

  2.icon:指定该Activity对应的图标

  3.lalbe:指定Activity的标签

注意:除此之外,配置Activity时,通常还需要指定一个或多个<intent-filter/>元素,该元素用于指定activity可响应的Intent.

配置方式如下图所示:

android学习笔记26——Activity-LMLPHP

新添加的XXActivity需要继承Activity.

Activity

ListActivity——实现列表

TabActivity——实现标签页

LaucherActivity

==》

继承LaucherActivity时通常应该重写Intent intentForPosition(int position)方法,该方法根据不同列表项返回不同的Intent(用于启动不同的Activity).

android学习笔记26——Activity-LMLPHP

实例:

操作步骤:

  1.res下添加xml文件夹,添加preference.xml

  2.添加OtherActivity、PreferenceActivityTest、ExpandableListActivityTest

  3.values下添加array.xml

  4.AndroidMainfest.xml添加Activity配置

资源文件==》
<?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="选择铃声"
android:title="设置铃声" >
</RingtonePreference> <PreferenceCategory android:title="个人信息设置组" > <!-- 通过输入框填写用户名 --> <EditTextPreference
android:dialogTitle="您所使用的用户名为: "
android:key="name"
android:summary="填写你的用户名"
android:title="用户名" />
<!-- 通过列表框选择性别 --> <ListPreference
android:dialogTitle="ListPreference"
android:entries="@array/gender_name_list"
android:entryValues="@array/gender_value_list"
android:key="gender"
android:summary="选择您的性别"
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"?>
<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> 代码实现==》
package com.example.mylauncheractivity; import android.app.LauncherActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter; 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 com.example.mylauncheractivity; 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.plane, R.drawable.plane3, R.drawable.plane5 };
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()
{
@SuppressWarnings("deprecation")
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, 64);
TextView tv = new TextView(ExpandableListActivityTest.this);
tv.setLayoutParams(lp);
tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
tv.setPadding(35, 0, 0, 0);
tv.setHeight(500);
tv.setTextSize(18);
return tv;
} 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);
}
} package com.example.mylauncheractivity; import android.os.Bundle;
import android.preference.PreferenceActivity; public class PreferenceActivityTest extends PreferenceActivity {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//设置显示参数设置布局
addPreferencesFromResource(R.xml.preference);
} }
AndroidMainfest.xml配置文件==》
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mylauncheractivity"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mylauncheractivity.OtherActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.mylauncheractivity.PreferenceActivityTest" />
<activity android:name="com.example.mylauncheractivity.ExpandableListActivityTest" />
</application> </manifest>

资源文件:图片,略.

运行效果:

android学习笔记26——Activity-LMLPHP  android学习笔记26——Activity-LMLPHP  android学习笔记26——Activity-LMLPHP

LauncherActivity可用于开发启动Activity列表。  

LauncherActivity继承自ListActivity,因此其本质就用于开发列表界面的Activity,其开发的列表界面中的每个列表项都对应一个Intent对象,因此当用户单击不同的列表项时,应用程序会自动启动对应的Activity.

LauncherActivity同样需要设置Adapter(可使用简单的ArrayAdapter/SimpleAdapter/扩展BaseAdapter实现自己的Adapter).与普通的ListActivity,不同的是,继承LauncherActivity时——通常应该重写Intent

intentForPosition(int position)方法,该方法根据不同列表返回不同的Intent——用于启动不同的Activity.

ExpandableListActivity,用于显示一个可展开的列表窗口。

PreferenceActivity,用于显示一个显示设置选项参数并进行保存的窗口。

04-20 20:47