最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

通過實例簡單講解Android App中的Activity組件

 更新時間:2016年04月24日 14:28:02   作者:劍蕭舞蝶  
這篇文章主要介紹了通過Android App中的Activity組件,包括Activity的定義和繼承以及啟動等基本知識,需要的朋友可以參考下

Activity是Android應(yīng)用中,最直接與用戶接觸的組件,它負(fù)責(zé)加載View組件,使其展現(xiàn)給用戶,并保持與用戶的交互。所有的Activity組件均需要繼承Activity類,這是一個Content的間接子類,包裝了一些Activity的基本特性。

View組件是所有UI組件、容器組件的基類,也就是說,它可以是一個布局容器,也可以是一個布局容器內(nèi)的基本UI組件。View組件一般通過XML布局資源文件定義,同時Android系統(tǒng)也對這些View組件提供了對應(yīng)的實現(xiàn)類。如果需要通過某個Activity把指定的View組件顯示出來,調(diào)用Activity的setContentView()方法即可,它具有多個重載方法,可以傳遞一個XML資源ID或者View對象。

例如

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

或者

setContentView(R.layout.main);

Activity為Android應(yīng)用提供了一個用戶界面,當(dāng)一個Ac-tivity被開啟之后,它具有自己的生命周期。Activity類也對這些生命周期提供了對應(yīng)的方法,如果需要對Activity各個不同的生命周期做出響應(yīng),可以重寫這些生命周期方法實現(xiàn)。對于大多數(shù)商業(yè)應(yīng)用而言,整個系統(tǒng)中包含了多個Activity,在應(yīng)用中逐步導(dǎo)航跳轉(zhuǎn)開啟這些Activity之后,會形成Activity的回退棧,當(dāng)前顯示并獲得焦點(diǎn)的Activity位于這個回退棧的棧頂。

實例

一、如何在定義多個Activity
1. 定義一個類來繼承Activty
2.調(diào)用Oncreate方法
3.在Anroidmianifest.xml中進(jìn)行注冊

二、如何啟動一個Activity
1.生成一個意圖對象(也就是Intent)
2.調(diào)用Intent對象的SetClass方法
3.調(diào)用當(dāng)前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> 

在創(chuàng)建第二個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的第一個方法的參數(shù)是一個Context 一個參數(shù)是表示你要啟動那個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)

相關(guān)文章

  • android APP登陸頁面適配的實現(xiàn)

    android APP登陸頁面適配的實現(xiàn)

    這篇文章主要介紹了android APP登陸頁面適配的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • Android 下的 QuickJS Binding 庫特性使用詳解

    Android 下的 QuickJS Binding 庫特性使用詳解

    這篇文章主要介紹了Android 下的 QuickJS Binding 庫特性使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Android Handler 的用法指南

    Android Handler 的用法指南

    Handler 是 Android 中用于線程間通信的重要機(jī)制,主要用于在不同線程之間發(fā)送和處理消息,下面給大家分享Android Handler 的用法指南,感興趣的朋友一起看看吧
    2025-04-04
  • Android Activity 入門簡介

    Android Activity 入門簡介

    Activity 是一個應(yīng)用組件,用戶可與其提供的屏幕進(jìn)行交互,以執(zhí)行撥打電話、拍攝照片、發(fā)送電子郵件或查看地圖等操作,這篇文章主要介紹了Android Activity入門基礎(chǔ)知識,需要的朋友可以參考下
    2024-04-04
  • Android webview手動校驗https證書(by 星空武哥)

    Android webview手動校驗https證書(by 星空武哥)

    有些時候由于Android系統(tǒng)的bug或者其他的原因,導(dǎo)致我們的webview不能驗證通過我們的https證書,最明顯的例子就是華為手機(jī)mate7升級到Android7.0后,手機(jī)有些網(wǎng)站打不開了,而更新了webview的補(bǔ)丁后就沒問題了
    2017-09-09
  • Android 仿QQ頭像自定義截取功能

    Android 仿QQ頭像自定義截取功能

    在我們的qq聊天工具中,經(jīng)常會使用qq頭像截取功能,基于代碼是怎么實現(xiàn)的呢?下面小編通過本文給大家分享android 仿qq頭像自定義截取功能的思路分析及編碼實現(xiàn)過程,感興趣的朋友一起學(xué)習(xí)吧
    2016-10-10
  • Android簡單實現(xiàn)自定義彈框(PopupWindow)

    Android簡單實現(xiàn)自定義彈框(PopupWindow)

    本文主要介紹了Android利用PopupWindow實現(xiàn)自定義彈框的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • Android四種常見布局方式示例教程

    Android四種常見布局方式示例教程

    Android四種布局有線性布局LinearLayout、相對布局RelativeLayout、網(wǎng)格布局GridLayout、和滾動視圖ScrollView,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-09-09
  • android上一個可追蹤代碼具體到函數(shù)某行的日志類

    android上一個可追蹤代碼具體到函數(shù)某行的日志類

    追蹤代碼到函數(shù)具體某行,這樣的功能,是每一個程序員都希望會有的,因為它可以幫助我們追蹤到某行代碼的錯誤,接下來介紹下android上一個可追蹤代碼到函數(shù)具體某行的日志類,希望對開發(fā)者有所幫助
    2012-12-12
  • android電源信息查看(電量、溫度、電壓)實例代碼

    android電源信息查看(電量、溫度、電壓)實例代碼

    這篇文章主要介紹了android電源信息查看方法,以實例形式較為詳細(xì)的分析了Android實現(xiàn)電源電量、電壓、溫度等信息查看的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10

最新評論

青龙| 洱源县| 玉环县| 兴文县| 长治市| 遂川县| 阳西县| 那曲县| 孟村| 石渠县| 雷山县| 鹤峰县| 贡山| 贵州省| 扬中市| 平泉县| 闽清县| 岐山县| 台湾省| 浑源县| 湟中县| 北碚区| 定西市| 玉环县| 留坝县| 桦甸市| 赣州市| 大悟县| 桐庐县| 墨脱县| 苗栗县| 东乡族自治县| 扶风县| 称多县| 托里县| 乐安县| 玉环县| 织金县| 克东县| 德江县| 祁门县|