Android 学习 — Activity

    网络上盛传Android 有四大天王,分别有: Activity,Service, Intent,BroadcastReceiver。 我今天就针对 Activity 说说俺的看法。
      An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of 
    creating a window for you in which you can place your UI with setContentView(View). While activities are often presented to the user as
    full-screen windows, they can also be used in other ways: as floating windows (via a theme with windowIsFloating set) or embedded inside of
    another activity (using ActivityGroup). There are two methods almost all subclasses of Activity will implement:
      onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource 
        defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.
      onPause() is where you deal with the user leaving your activity. Most importantly, any changes made by the user should at this point be 
        committed (usually to the ContentProvider holding the data).   To be of use with Context.startActivity(), all activity classes must have a corresponding <activity> declaration in their package's
    AndroidManifest.xml.
    上段话呢,直接摘自官方的帮助文档,大致的意思是说:Activity 代表一个用户所能看到的屏幕,主要用于处理应用程序的整体性工作,例如,监听系统事
    件(按键事件、触摸屏事件等),为用户显示指定的View,启动其他Activity 等。所有应用的Activity 都继承于android.app.Activity 类,该类是Android 提
    供的基层类,其他的Activity 继承该父类后,通过父类的方法来实现各种功能。
    OnCreate( Bundle ) 函数是初始化你的Activity。它通过调用setContentView(int) 来定义你的UI,并且通过findViewById(int)函数来获取你在UI中定
    义的小控件。
    在使用一个Activity类的时候,要注意六个应用方法,分别为onCreate,onStart,onResume,onPause,onStop,onDestory等.每一个Activity都有一个窗口,在默认情况下他是充满整个屏幕的,当然他的大小可以设置, 或许比屏幕小,获取可以悬浮到其他的Activity上。在Activity上的可视化空间都是有view及其子类组成的,按照布局文件中制定的位置上的窗口进行摆放。好了, 举个例子吧!
    要有继承一个Activity类。 
    1 package com.amir.activity;
    2
    3 import android.app.Activity;
    4 import android.content.Intent;
    5 import android.os.Bundle;
    6 import android.view.View;
    7 import android.view.View.OnClickListener;
    8 import android.widget.Button;
    9 import android.widget.TextView;
    10
    11 public class Activite01 extends Activity {
    12 Button test01 = null;
    13 TextView textView01 = null;
    14
    15 public void onCreate(Bundle savedInstanceState) {
    16 super.onCreate(savedInstanceState);
    17 setContentView(R.layout.main);
    18 test01 = (Button)findViewById(R.id.test01);
    19 test01.setText(R.string.test01);
    20 test01.setOnClickListener(new test01OnClickListener());
    21
    22 textView01 = (TextView)findViewById(R.id.textView01);
    23 textView01.setText(R.string.hello);
    24 }
    25
    26 class test01OnClickListener implements OnClickListener {
    27
    28 public void onClick(View arg0) {
    29
    30 Intent ent = new Intent();
    31 ent.setClass(Activite01.this, Activity02.class);
    32 Activite01.this.startActivity(ent);
    33 }
    34
    35 }
    36 }

    看看AndroidManifest.xml要怎样写:
    1 <?xml version="1.0" encoding="utf-8"?>
    2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3 package="com.amir.activity" android:versionCode="1"
    4 android:versionName="1.0">
    5 <uses-sdk android:minSdkVersion="8" />
    6
    7 <application android:icon="@drawable/icon" android:label="@string/app_name">
    8 <activity android:name=".Activite01" android:label="@string/app_name">
    9 <intent-filter>
    10 <action android:name="android.intent.action.MAIN" />
    11 <category android:name="android.intent.category.LAUNCHER" />
    12 </intent-filter>
    13 </activity>
    14 <activity android:name=".Activity02" android:label="@string/activity02">
    15 </activity>
    16
    17 </application>
    18 </manifest>

    其中 <intent-filter> ,这个标签里的信息说明,默认显示的Activity。说明这个Activity 是软件默认启动。
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    在Androidmainfest.xml 中声明一个activity:
    <activity android:name=".Activity02" android:label="@string/activity02">
    </activity>

原文地址:https://www.cnblogs.com/nobileamir/p/1966542.html