標籤:mat 前言 命中 color 回顧 enter set end src
前言:生命中有很多人陪伴自己走過一生中的某段旅程,僅僅是有些人僅僅是某階段出現,有些人卻陪伴自己非常久。就像小學、中學、高中、大學,那些以前以為會長久擁有的,當經曆過天涯各地地忙碌於生活,或如意。或失意;漸漸地那些畫面僅僅剩回顧。天涯各自安命,能在一起的就儘力珍惜吧,不在一起地就衷心地祝福,我們都須要一種姿態生活下去!Android中的Activity的生命中,我們常常須要開啟另外一個Activity,即另外一個介面。這個可能出現的時間非常短暫,然後又回到主介面。可是這兩個Activity都在各自的生命週期中經曆了生命中的某個階段。
該項目在上篇項目中加入相關內容,進行示範。項目結構例如以下:
第一步:在layout中加入另外一個介面布局,即activity_sub.xml,代碼內容例如以下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/close" android:layout_below="@id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/close" /></RelativeLayout>
當然主介面activity_sub.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"> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/open" android:layout_below="@id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/open" /></RelativeLayout>
第二步:在values檔案夾下的strings.xml中編寫代碼內容例如以下:
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">TestActivity</string> <string name="hello_world">Test Activity life cycle</string> <string name="close">關閉</string> <string name="open">開啟另外一個介面</string></resources>
註:這個檔案裡的東西是layout介面布局中要用到的東西;比方android:text="@string/open"中的open就是相應<string name="open">開啟另外一個介面</string>,這個介面布局完後,程式介面button就會顯示:開啟另外一個介面
第三步:在src中建立一個檔案SubActivity.java,代碼編寫例如以下:
package com.wyz.testactivity;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class SubActivity extends Activity {private static final String TAG="SubActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_sub);Log.d(TAG, "生命週期--建立階段(onCreate)");Button btn_close = (Button)findViewById(R.id.close);btn_close.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {SubActivity.this.finish();//關閉}});}protected void onStart(){super.onStart();Log.d(TAG, "生命週期--開始階段(onStart)");}protected void onResume(){super.onResume();Log.d(TAG, "生命週期--恢複階段(onResume)");}protected void onPause(){super.onPause();Log.d(TAG, "生命週期--暫停建立階段(onPause)");}protected void onStop(){super.onStop();Log.d(TAG, "生命週期--停止建立階段(onStop)");}protected void onRestart(){super.onRestart();Log.d(TAG, "生命週期--重新啟動階段(onRestart)");}protected void onDestory(){super.onDestroy();Log.d(TAG, "生命週期--銷毀階段(onDestory)");}}
接著我們也要改動下MainActivity.java,讓它能夠開啟子介面:
package com.wyz.testactivity;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private static final String TAG="MainActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.d(TAG, "生命週期--建立階段(onCreate)");Button btn_open = (Button)findViewById(R.id.open);btn_open.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {startActivity(new Intent(MainActivity.this, SubActivity.class));//開啟另外一個介面}});}protected void onStart(){super.onStart();Log.d(TAG, "生命週期--開始階段(onStart)");}protected void onResume(){super.onResume();Log.d(TAG, "生命週期--恢複階段(onResume)");}protected void onPause(){super.onPause();Log.d(TAG, "生命週期--暫停建立階段(onPause)");}protected void onStop(){super.onStop();Log.d(TAG, "生命週期--停止建立階段(onStop)");}protected void onRestart(){super.onRestart();Log.d(TAG, "生命週期--重新啟動階段(onRestart)");}protected void onDestory(){super.onDestroy();Log.d(TAG, "生命週期--銷毀階段(onDestory)");}}
最後一步:改動下AndroidManifest.xml,在</application>節點前加入內容例如以下:
<activity android:name=".SubActivity" android:label="@string/app_name" > </activity>
這個加入的作用是:讓程式知道有另外一個介面存在了!
註:android:name就是子介面的類名
介面效果例如以下:
【Android開發-8】生命週期,Activity中開啟另外一個Activity