通过实例简单讲解Android App中的Activity组件

Activity是Android应用中,最直接与用户接触的组件,它负责加载View组件,使其展现给用户,并保持与用户的交互。所有的Activity组件均需要继承Activity类,这是一个Content的间接子类,包装了一些Activity的基本特性。

View组件是所有UI组件、容器组件的基类,也就是说,它可以是一个布局容器,也可以是一个布局容器内的基本UI组件。View组件一般通过XML布局资源文件定义,同时Android系统也对这些View组件提供了对应的实现类。如果需要通过某个Activity把指定的View组件显示出来,调用Activity的setContentView()方法即可,它具有多个重载方法,可以传递一个XML资源ID或者View对象。

例如

LinearLayout layout=new LinearLayout(this);setContentView(layout);

或者

setContentView(R.layout.main);

Activity为Android应用提供了一个用户界面,当一个Ac-tivity被开启之后,它具有自己的生命周期。Activity类也对这些生命周期提供了对应的方法,如果需要对Activity各个不同的生命周期做出响应,可以重写这些生命周期方法实现。对于大多数商业应用而言,整个系统中包含了多个Activity,在应用中逐步导航跳转开启这些Activity之后,会形成Activity的回退栈,当前显示并获得焦点的Activity位于这个回退栈的栈顶。

实例

一、如何在定义多个Activity 1. 定义一个类来继承Activty2.调用Oncreate方法3.在Anroidmianifest.xml中进行注册

二、如何启动一个Activity1.生成一个意图对象(也就是Intent)2.调用Intent对象的SetClass方法3.调用当前Activity继承类中的startActiviyt的方法

三、代码一个Activity的xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:tools="http://schemas.android.com/tools"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:paddingBottom="@dimen/activity_vertical_margin"   android:paddingLeft="@dimen/activity_horizontal_margin"   android:paddingRight="@dimen/activity_horizontal_margin"   android:paddingTop="@dimen/activity_vertical_margin"   tools:context=".MainActivity" >   <Button      android:id="@+id/button1"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:text="启动一个Activity"/>  </RelativeLayout> 

第二个Activity的xml文件

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:orientation="vertical" >      <TextView      android:id="@+id/textview1"     android:layout_height="wrap_content"     android:layout_width="match_parent"     android:text="第二个activity"     android:gravity="center_horizontal"/>    </LinearLayout> 

在创建第二个xml文件是在Androidmianifest.xml文件中注册

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"   package="com.android.xiong.moreactivity"   android:versionCode="1"   android:versionName="1.0" >    <uses-sdk     android:minSdkVersion="8"     android:targetSdkVersion="17" />    <application     android:allowBackup="true"     android:icon="@drawable/ic_launcher"     android:label="@string/app_name"     android:theme="@style/AppTheme" >     <activity       android:name="com.android.xiong.moreactivity.MainActivity"       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.android.xiong.moreactivity.SecondActivity"       android:label="secondActivity">     </activity>   </application>  </manifest> 

启动第二个activity

package com.android.xiong.moreactivity;  import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;  public class MainActivity extends Activity {      private Button button1;    @Override   protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);     button1=(Button)findViewById(R.id.button1);     OnClick onc=new OnClick();     button1.setOnClickListener(onc);   }         class OnClick implements OnClickListener{          /**      * setClass的第一个方法的参数是一个Context 一个参数是表示你要启动那个Activity 是一个class对象      * Context是activity的父类      */     @Override     public void onClick(View v) {       // TODO Auto-generated method stub       Intent intent=new Intent();       intent.setClass(MainActivity.this, SecondActivity.class);       MainActivity.this.startActivity(intent);                   }        }   @Override   public boolean onCreateOptionsMenu(Menu menu) {     // Inflate the menu; this adds items to the action bar if it is present.     getMenuInflater().inflate(R.menu.main, menu);     return true;   } } 

爱情不是避难所,想进去避难的话,是会被赶出来的。

通过实例简单讲解Android App中的Activity组件

相关文章:

你感兴趣的文章:

标签云: