发布于 2016-06-21 13:37:56 | 79 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了通过Android App中的Activity组件,包括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. 定义一个类来继承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; 
  } 


} 



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务