当前位置: 移动技术网 > 移动技术>移动开发>Android > 通过实例简单讲解Android App中的Activity组件

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

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)

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网