Activity範例說明

Download Report

Transcript Activity範例說明

第二章
手機與PDA行動裝置平台簡介
實驗介紹
• 實驗目的
– 撰寫程式 “EX03_09”
– 利用Intent將主Activity前往Activity2,並在
Activity2建立一個回到Activity1的按鈕。
– 主要意義是示範如何在一個Activity中呼叫
另一個Activity的方法。
實驗環境
• 開發環境
– Eclipse 3.5
– Android 2.0.1
– JDK 6 以上
實驗步驟
• 首先打開 Eclipse 3.5
– File → New→ Android Project
專案初始畫面
EX03_09
• 撰寫“EX03_09”程式
• 在模擬器中執行
• 情境:
–從原本介面1到介面2,
再從介面2回介面1
介面2
介面1
介面1
介面 main.xml(1)
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
//絕對位置版面配置(座標佈局)
android:layout_width="fill_parent“
//寬度填滿
android:layout_height="fill_parent“
//長度填滿
android:background=“@drawable/black“
//從color.xml讀取black代表的值,其值代表顏色
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
//加入文字顯示物件
android:id=“@+id/text1“
//設定此TextView物件名稱為text1
android:textSize=“24sp“
//設定TextView文字大小
android:layout_width=“186px“
//設定TextView寬度
android:layout_height=“29px“
//設定TextView高度
android:layout_x=“70px“
//設定TextView座標x
android:layout_y=“32px“
//設定TextView座標y
android:text=“@string/act1“
//從string.xml讀取act1代表的文字
/>
<Button
//放入按鈕物件
android:id=“@+id/button1“
//設定此按鈕物件名稱為button1
android:layout_width=“118px“
//設定按鈕的寬度
android:layout_height=“wrap_content“
//設定按鈕的高度(wrap_content會放入適當的高度)
android:layout_x=“100px“
//設定按鈕的座標x
android:layout_y=“82px“
//設定按鈕的座標y
android:text=“Go to Activity2“
//設定按鈕顯示的文字為” Go to Activity2”
/>
</AbsoluteLayout>
介面 main.xml(2)
•
•
•
•
TextView
TextView物件text1
Button
Button物件button1
介面 mylayout.xml(1)
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
//絕對位置版面配置(座標佈局)
android:layout_width="fill_parent“
//寬度填滿
android:layout_height="fill_parent“
//長度填滿
android:background=“@drawable/white “
//從color.xml讀取white代表的值,其值代表顏色(白)
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
//加入文字顯示物件
android:id=“@+id/text2“
//設定此TextView物件名稱為text2
android:textSize=“24sp“
//設定TextView文字大小
android:layout_width=“186px“
//設定TextView寬度
android:layout_height=“29px“
//設定TextView高度
android:layout_x=“70px“
//設定TextView座標x
android:layout_y=“32px“
//設定TextView座標y
android:textColor=“@drawable/black“ //設定TextView的文字讀取color.xml中black的值(黑色)
android:text=“@string/act2“
//從string.xml讀取act2代表的文字
/>
<Button
//放入按鈕物件
android:id=“@+id/button2“
//設定此按鈕物件名稱為button2
android:layout_width=“118px“
//設定按鈕的寬度
android:layout_height=“wrap_content“
//設定按鈕的高度(wrap_content會放入適當的高
度)
android:layout_x=“100px“
//設定按鈕的座標x
android:layout_y=“82px“
//設定按鈕的座標y
android:text=“Go to Activity1“
//設定按鈕顯示的文字為” Go to Activity1”
/>
</AbsoluteLayout>
介面 main.xml(2)
•
•
•
•
TextView
TextView物件text2
Button
Button物件button2
Android程式碼 EX03_09.java
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
Import略
public class EX03_09 extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* 載入main.xml Layout */
setContentView(R.layout.main);
/* 以findViewById()取得Button物件,並加入onClickListener */
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
/* new一個Intent物件,並指定要啟動的class */
Intent intent = new Intent();
intent.setClass(EX03_09.this, EX03_09_1.class);
/* 呼叫一個新的Activity */
startActivity(intent);
/* 關閉原本的Activity */
EX03_09.this.finish();
}
});
}
}
Android程式碼 EX03_09_1.java
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
Import略
public class EX03_09_1 extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* 載入mylayout.xml Layout */
setContentView(R.layout.mylayout);
/* 以findViewById()取得Button物件,並加入onClickListener */
Button b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
/* new一個Intent物件,並指定要啟動的class */
Intent intent = new Intent();
intent.setClass(EX03_09_1.this, EX03_09.class);
/* 呼叫一個新的Activity */
startActivity(intent);
/* 關閉原本的Activity */
EX03_09_1.this.finish();
}
});
}
}