2010年2月8日星期一

活動(activity)和意圖(intent)

一個活動(Activity)是一個用戶可以做的單一的,集中的事. 幾乎所有的活動都與用戶交互,活動類負責創建一個窗口(window), 可以通過setContentView(View)放置用戶界面(UI). 雖然活動往往呈現全屏幕窗口給用戶,但也可以其他方式呈現: 通過windowIsFloating的主題呈現浮動窗口, 或使用ActivityGroup嵌入到另一個活動中.

意圖(Intent)是一個要執行的操作的抽象描述. 它為不同的應用代碼之間提供後期運行時綁定(late runtime binding). 這基本上是一個被動數據結構, 它持有一個要執行的操作的抽象描述.

startActivity是啟動一項新活動的方法, 將不會收到任何有關活動退出時的信息.

在這實例中, 主活動通過startActivity啟動另一個活動, 為了簡單, 這實例中不會在活動之間傳遞數據.



在一個應用程序, 如果需要一個額外的活動, 需要在清單文件(AndroidManifest.xml)中申報.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AndroidIntent"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidIntent"
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=".Activity2" />
</application>
<uses-sdk android:minSdkVersion="7" />

</manifest>


創建一個新的活動Activity2.java和相應的佈局layout2.xml

Activity2.java
package com.AndroidIntent;
import android.app.Activity;
import android.os.Bundle;


public class Activity2 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
}

}


layout2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="It's Activity2"
/>
</LinearLayout>


修改主佈局(main.xml), 添加一個按鈕來啟動另一個活動.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Launch Activity2"
android:id="@+id/launchbutton"
/>
</LinearLayout>


修改主活動代碼
package com.AndroidIntent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AndroidIntent extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button launchButton = (Button)findViewById(R.id.launchbutton);
launchButton.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent newIntent = new Intent(AndroidIntent.this, Activity2.class);
startActivity(newIntent);
}});
}
}


相關文章: 包裹(Bundle)



沒有留言:

發佈留言