android入門——Activity(1)

來源:互聯網
上載者:User

標籤:

結構圖

mainfests檔案夾下面有一個AndroidMainfest.xml檔案,類似web開發中的web.xml檔案負責整個項目的配置,每當我們建立一個activity,都要在這個檔案中進行配置。

java檔案夾類似src。下面存放原始碼。

res檔案夾放資源檔 layout是布局檔案檔案夾,mipmap存放表徵圖,values存放索引值。

 

建立Layout resource file

<?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:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Hello blue" />    <Button        android:id="@+id/btn_start_second"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="啟動第二個介面"/></LinearLayout>
activity_my.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Hello second"/>    <Button        android:id="@+id/btn_finish_self"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="點擊"/></LinearLayout>
activity_second.xml

 

建立class

package com.ouc.wkp.activitylesson1;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;/** * Created by wkp on 2016/8/19. */public class MyActivity extends Activity {    Button btnStart;    //建立activity時調用    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        Log.e("MyActivity","MyActivity_oncreate");        setContentView(R.layout.activity_my);        btnStart = (Button) findViewById(R.id.btn_start_second);        btnStart.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                //在這裡啟動第二個activity                //顯示啟動的第一種寫法//                Intent intent=new Intent();//                intent.setClass(MyActivity.this,SecondActivity.class);//                startActivity(intent);                //顯示啟動的第二種寫法//                Intent intent1=new Intent();//                intent1.setClassName(MyActivity.this,"com.ouc.wkp.activitylesson1.SecondActivity");//                startActivity(intent1);                //顯示啟動的第三種寫法//                Intent intent2=new Intent();//                ComponentName componentName=new ComponentName(MyActivity.this,SecondActivity.class);//                intent2.setComponent(componentName);//                startActivity(intent2);                //隱試啟動 第一種寫法//                Intent intent3=new Intent("abcd.SecondActivity");//                startActivity(intent3);                //隱試啟動 第二種寫法                Intent intent4=new Intent();                intent4.setAction("abcd.SecondActivity");                startActivity(intent4);            }        });    }    //當activity介面變為使用者可見時調用    @Override    protected void onStart() {        super.onStart();        Log.e("MyActivity","MyActivity_onstart");    }    //當activity介面擷取到焦點時調用(介面按鈕可點擊,文字框可輸入)    @Override    protected void onResume() {        super.onResume();        Log.e("MyActivity","MyActivity_onresume");    }    //當activity失去焦點(按鈕不可點,文字框不能輸入)    @Override    protected void onPause() {        super.onPause();        Log.e("MyActivity","MyActivity_onpause");    }    //當activity變為不可見時調用    @Override    protected void onStop() {        super.onStop();        Log.e("MyActivity","MyActivity_onstop");    }    //當activity被銷毀時調用    @Override    protected void onDestroy() {        super.onDestroy();        Log.e("MyActivity","MyActivity_ondestroy");    }}
MyActivity.java

 

配置一下AndroidMainfest.xml檔案

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.ouc.wkp.activitylesson1">    <!--icon應用程式圖示 label應用程式名稱 theme主題-->    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity            android:name=".MyActivity"            android:label="ppp"            android:theme="@style/AlertDialog.AppCompat">            <intent-filter>                <!--主activity-->                <action android:name="android.intent.action.MAIN" />                <!--啟動策略-->                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <!-- 四種啟動模式standard singleTop  singleTask-->        <activity            android:name=".SecondActivity"            android:label="第二個activity"            android:launchMode="singleTop">            <intent-filter>                <action android:name="abcd.SecondActivity" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>        </activity>        <activity android:name=".ThirdActivity">            <intent-filter>                <action android:name="abcd.ThirdActivity" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>        </activity>    </application></manifest>
AndroidMainfest.xml
我們通過設定MAIN和LAUNCHER吧MyActivity定義為主顯示視窗
<intent-filter>
<!--主activity-->
<action android:name="android.intent.action.MAIN" />
<!--啟動策略-->
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>


我們重寫onCreate()方法,

btnStart = (Button) findViewById(R.id.btn_start_second);

擷取資源檔中定義的Button,為這個按鈕添加點擊事件,

btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
  //這裡添加點擊事件
}
});

我們看到MyActivity.java中有五種啟動方式。運行程式點擊按鈕後會跳轉到SecondActivity.

如果有兩個activity的action name相同,比如都是

<intent-filter>
<action android:name="abcd.SecondActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
那麼點擊按鈕後會提供兩個選擇給使用者



我們可以給SecondActivity添加點擊事件
SecondActivity.this.finish();來返回MyActivity.


Activity的生命週期有6個函數

onCreate 建立activity時調用

onStart   當activity介面變為使用者可見時調用

onResume當activity介面擷取到焦點時調用(介面按鈕可點擊,文字框可輸入)

onPause  當activity失去焦點(按鈕不可點,文字框不能輸入)

onStop   當activity變為不可見時調用

onDestroy當activity銷毀時調用

 

運行程式後執行順序為

myactivity->oncreate onstart onresume  

點擊跳轉按鈕  myactivity->onpause   secondactivity->oncreate onstart onresume   myactivity->onstop

點擊關閉按鈕  secondActivity->onpause   myactivity->onstart onresume  

secondactivity->stop destroy

 

上面的代碼不斷調試 可能有些地方和描述不符合

 

activity可以設定launchMode 有四種stardard singleTop singleTask singleInstance

singletop時,安卓系統會判斷當前棧頂顯示的activity是不是要開啟的Activity,如果不是,那麼系統會建立一個新的Activity,如果是就不會

singleTask時,和singletop的區別是系統判斷的是棧中而不是棧頂

singleInstance時,新的taskId。

 

按下手機返回鍵時,原activity從task棧頂彈出。

android入門——Activity(1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.