国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

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

2019-12-12 06:30:50
字體:
來源:轉載
供稿:網友

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)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嫩江县| 江山市| 承德市| 南川市| 鸡泽县| 同心县| 哈密市| 什邡市| 怀安县| 隆德县| 阜康市| 洱源县| 盐池县| 水富县| 虎林市| 大悟县| 平武县| 惠来县| 阳春市| 霍邱县| 鹿泉市| 民丰县| 安泽县| 荔波县| 安吉县| 准格尔旗| 古浪县| 平潭县| 西吉县| 手机| 新乡县| 余庆县| 凤翔县| 罗甸县| 建宁县| 吉木萨尔县| 宣化县| 三台县| 太康县| 英超| 麻江县|