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.

配置方式如下图所示:

新添加的XXActivity需要继承Activity.

Activity

ListActivity——实现列表

TabActivity——实现标签页

LaucherActivity

==》

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

实例:

操作步骤:

  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>

资源文件:图片,略.

运行效果:

    

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

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

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

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

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

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

android学习笔记26——Activity的更多相关文章

  1. Android学习笔记之Activity详解

    1 理解Activity Activity就是一个包含应用程序界面的窗口,是Android四大组件之一.一个应用程序可以包含零个或多个Activity.一个Activity的生命周期是指从屏幕上显示那 ...

  2. Android学习笔记:Activity生命周期详解

    进行android的开发,必须深入了解Activity的生命周期.而对这个讲述最权威.最好的莫过于google的开发文档了. 本文的讲述主要是对 http://developer.android.co ...

  3. android学习笔记28——Activity生命周期

    Activity生命周期 Activity的活动状态由android已Activity栈的形式管理,当前活动的Activity位于栈顶.随着不同应用的运行,每个Activity都有可能从活动状态转入非 ...

  4. android学习笔记27——Activity

    Activity配置==> android应用程序要求所有的应用程序组件都需要进行显示配置后,才可正常使用.包括:Activity.Service.BroadCastReceiver.Conte ...

  5. [Android学习笔记]设置Activity方向

    1.设置Activity方向 在AndroidMainfest.xml里设置Activity默认方向 <activity android:name=".myActivity" ...

  6. Android学习笔记4——Activity详解

    在 Android 开发过程中,与程序员打交道最多的应该就是作为四大组件之一的 Activity 了.接下来我们就一起来揭开 Activity 的神秘面纱吧~ 一.概述 什么是 Activity(活动 ...

  7. android学习笔记(5)Activity生命周期学习

    每一个activity都有它的生命周期,开启它,关闭它,跳转到其他activity等等,都会自己主动调用下面某种方法.对这些个方法覆写后观察学习. protected void onCreate(Bu ...

  8. android学习笔记三--Activity 布局

    1.线性布局 标签 :<LinearLayout></LinearLayout> 方向:android:orientation, 垂直:vertical 水平:Horizont ...

  9. Pro Android学习笔记(一三七):Home Screen Widgets(3):配置Activity

    文章转载仅仅能用于非商业性质,且不能带有虚拟货币.积分.注冊等附加条件.转载须注明出处http://blog.csdn.net/flowingflying/以及作者@恺风Wei. 通过widget定义 ...

随机推荐

  1. android获取inflater

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: ? LayoutInflater inflater=(La ...

  2. Android中GC_EXTERNAL_ALLOC的含义

    GC_FOR_MALLOC means that the GC was triggered because there wasn't enough memory left on the heap to ...

  3. Java-->把txt中的所有字符按照码表值排序

    --> List 列表中的自动添加的多余空间长度该怎么去除呢?...(已解决,是char 数组中的空字符) package com.dragon.java.filesort; import ja ...

  4. Codeforces Round #377 (Div. 2) A B C D 水/贪心/贪心/二分

    A. Buy a Shovel time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  5. leetcode 91 Decode Ways ----- java

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  6. Apache的https协议配置

    一.http协议和https协议的传输格式 http:文本格式的协议 https:二进制格式的协议 二.x509.3证书格式: 证书格式的版本号 证书序列号 证书签名算法 证书颁发者 有效期 持有者的 ...

  7. PHP避免刷新页面重复提交

    PHP避免刷新页面重复提交 2013-07-09 15:27 匿名 | 浏览 3567 次 编程语言 情景:从html提交数据到x.php 在x.php中$_POST数据写库并且显示,当x.php刷新 ...

  8. Jewelry Exhibition(最小点覆盖集)

    Jewelry Exhibition 时间限制: 1 Sec  内存限制: 64 MB提交: 3  解决: 3[提交][状态][讨论版] 题目描述 To guard the art jewelry e ...

  9. CentOS 中PHP开启 GD功能

    yum install php-gd 然后重启服务器: service httpd restart

  10. C++ 实用的小程序

    1. 打开test_ids.txt 将里面的东西添加"1_",然后另存为test_ids_repaired.txt #include <iostream> #inclu ...