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

所属分类: 软件编程 / Android 阅读数: 744
收藏 0 赞 0 分享

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. 定义一个类来继承Activty
2.调用Oncreate方法
3.在Anroidmianifest.xml中进行注册

二、如何启动一个Activity
1.生成一个意图对象(也就是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; 
  } 


} 

2016424142946027.png (436×348)

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多