2015年2月17日 星期二

UI用戶界面

FrameLayout:是指定一塊區域,也就是說保留一個區塊放元件,所有放在上面的元件都會被放置在FrameLayout的左上角,如果說把多個元件一起放在FrameLayout中,那麼這些控制項會一個一個疊在一起。
sample_frame_layout.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">

    <!--FrameLayout主要用來呈現一個固定的區塊,這裡載入一張圖片,同時載入不同圖片將會被覆蓋-->
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <!--圖片檔名要英文,並且照片需放在mipmap的四個資料夾中-->
        <ImageView
            android:src="@mipmap/tr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView" />
    </FrameLayout>
</LinearLayout>
LinearLayout:是最常用的一種Layout,可設定成horizontal水平或者是vertical垂直左右排列。
sample_linear_layout.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">
    <!--這裡特別注意layout_weight被定成1,與下面的另一個layout的weight是一樣的,呈現時就會1:1-->
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="New Text"
            android:id="@+id/textView5"
            android:background="#ffff201b" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="New Text"
            android:id="@+id/textView6"
            android:background="#ff08ff1d" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="New Text"
            android:id="@+id/textView7"
            android:background="#ff1227ff" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="New Text"
            android:id="@+id/textView8"
            android:background="#ff940cff" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
       >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="9pt"
            android:text="New Text"
            android:id="@+id/textView" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12pt"
            android:text="New Text"
            android:id="@+id/textView2" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15pt"
            android:text="New Text"
            android:id="@+id/textView3" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18pt"
            android:text="New Text"
            android:id="@+id/textView4" />
    </LinearLayout>



</LinearLayout>

RelativeLayout:顧名思義就相對位置的版面配置。

sample_relative_layout.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">
    <!--RelativeLayout顧名思議就是相對位置-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="我是TextView控制項"
            android:id="@+id/textView9"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button"
            android:layout_below="@+id/textView9"
            android:layout_alignParentStart="true" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button2"
            android:layout_below="@+id/button"
            android:layout_centerHorizontal="true" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button3"
            android:layout_alignTop="@+id/button2"
            android:layout_alignParentEnd="true" />

    </RelativeLayout>

</LinearLayout>

TableLayout:顧名思義就像是表格般的版面配置

sample_table_layout.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">


    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Name"
                android:id="@+id/textView10" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="textPersonName"
                android:text="Name"
                android:ems="10"
                android:id="@+id/editText" />
        </TableRow>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TEL"
                android:id="@+id/textView11" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="textPersonName"
                android:text="Name"
                android:ems="10"
                android:id="@+id/editText2" />
        </TableRow>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/button4" />
        </TableRow>
    </TableLayout>
</LinearLayout>

2015年2月15日 星期日

Android常用控制項運用

範例說明
主畫面配置幾個Button控制項,每個Button按下去以後顯示另外的畫面呈現另外的基本控制項。

main.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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".AppMain">

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView" >

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="按鈕控制項"
                android:id="@+id/btn_Button"
                android:layout_below="@+id/textView"
                android:layout_toEndOf="@+id/scrollView" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="顯示文字控制項"
                android:id="@+id/btn_TextView"
                android:layout_below="@+id/button"
                android:layout_toEndOf="@+id/scrollView" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="編輯文字控制項"
                android:id="@+id/btn_Edit_Text"
                android:layout_below="@+id/button2"
                android:layout_toEndOf="@+id/scrollView" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="勾選式控制項"
                android:id="@+id/btn_Check_Box" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="點選式控制項"
                android:id="@+id/btn_Raid_Grop" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

AppMain.java

package com.example.win7.button_activity;

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


public class AppMain extends Activity {
    private Button btn_Button,btn_TextView,btn_Edit_Text,btn_Check_Box,btn_Raid_Grop;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setupViewComponent();
    }
    private void setupViewComponent(){
        btn_Button=(Button)findViewById(R.id.btn_Button);
        btn_TextView=(Button)findViewById(R.id.btn_TextView);
        btn_Edit_Text=(Button)findViewById(R.id.btn_Edit_Text);
        btn_Check_Box=(Button)findViewById(R.id.btn_Check_Box);
        btn_Raid_Grop=(Button)findViewById(R.id.btn_Raid_Grop);

        btn_Button.setOnClickListener(Press_Button);
        btn_TextView.setOnClickListener(Press_TexView);
        btn_Edit_Text.setOnClickListener(Press_EditText);
        btn_Check_Box.setOnClickListener(Press_CheckBox);
        btn_Raid_Grop.setOnClickListener(Press_Raid_Grop);
    }
    private Button.OnClickListener Press_Button = new Button.OnClickListener(){

        @Override
        public void onClick(View view) {
            /*Andioid切換畫面是用Intentj物件*/
            Intent intent_Button = new Intent();
            /*使用Intent的setclass()來設定目前以及將轉換的Activity*/
            intent_Button.setClass(AppMain.this,SampleButton.class);
            /*startActivity()方法將剛剛設定好的Intent傳遞進去*/
            startActivity(intent_Button);
        }
    };

    private Button.OnClickListener Press_TexView = new Button.OnClickListener(){

        @Override
        public void onClick(View view) {
            Intent intent_TextView = new Intent();
            intent_TextView.setClass(AppMain.this,SampleTextView.class);
            startActivity(intent_TextView);
        }
    };

    private Button.OnClickListener Press_EditText = new Button.OnClickListener(){

        @Override
        public void onClick(View view) {
            Intent intent_EdistText = new Intent();
            intent_EdistText.setClass(AppMain.this,SampleEditText.class);
            startActivity(intent_EdistText);
        }
    };

    private Button.OnClickListener Press_Raid_Grop = new Button.OnClickListener(){

        @Override
        public void onClick(View view) {
            Intent intent_RaidGrop = new Intent();
            intent_RaidGrop.setClass(AppMain.this,SampleRaidGrop.class);
            startActivity(intent_RaidGrop);
        }
    };

    private Button.OnClickListener Press_CheckBox = new Button.OnClickListener(){

        @Override
        public void onClick(View view) {
            Intent intent_CheckBox = new Intent();
            intent_CheckBox.setClass(AppMain.this,SampleCheckBox.class);
            startActivity(intent_CheckBox);
        }
    };
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_app_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

sample_button.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">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="點我點我"
        android:id="@+id/btn_Click" />

</LinearLayout>

SampleButton.java
package com.example.win7.button_activity;

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

/**
 * Created by win7 on 2015/2/15.
 */
public class SampleButton extends Activity {
    private Button btn_Click;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_button);
        setupViewComponent();
    }

    private void setupViewComponent() {
        btn_Click = (Button) findViewById(R.id.btn_Click);
        btn_Click.setOnClickListener(ClickButton);
    }

    private Button.OnClickListener ClickButton = new Button.OnClickListener() {

        @Override
        public void onClick(View view) {
            setTitle("Button以被點選");
        }
    };
}
sample_edit_text.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">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="352dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name"
            android:id="@+id/txv_Name" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/edt_Name"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="取得EditView中的內容"
            android:id="@+id/btn_Get_Edit_Text" />
    </LinearLayout>

</LinearLayout>

SampleEditText.java
package com.example.win7.button_activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by win7 on 2015/2/15.
 */
public class SampleEditText extends Activity {
    private Button btn_Get_Edit_Text;
    private TextView txv_Name;
    private EditText edt_Name;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_edit_text);
        setupViewComponent();
    }

    private void setupViewComponent() {
        btn_Get_Edit_Text = (Button) findViewById(R.id.btn_Get_Edit_Text);
        txv_Name = (TextView) findViewById(R.id.txv_Name);
        edt_Name = (EditText) findViewById(R.id.edt_Name);

        btn_Get_Edit_Text.setOnClickListener(Get_EditText);
    }

    private Button.OnClickListener Get_EditText = new Button.OnClickListener() {

        @Override
        public void onClick(View view) {
            setTitle("輸入的名稱為:"+edt_Name.getText());
        }
    };
}
sample_check_box.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">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Apple"
            android:id="@+id/cb_Apple" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Movie"
            android:id="@+id/cb_Movie" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TV"
            android:id="@+id/cb_TV" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="取得目前被選擇的值"
            android:id="@+id/btn_Select"/>
    </LinearLayout>
</LinearLayout>
SampleCheckBox.java
package com.example.win7.button_activity;

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

/**
 * Created by win7 on 2015/2/15.
 */
public class SampleCheckBox extends Activity {
    private CheckBox cb_Apple,cb_Movie,cb_TV;
    private Button btn_Select;
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_check_box);
        setTitle("這是CheckBox的Activity");
        setupViewComponent();
    }
    private void setupViewComponent(){
        cb_Apple=(CheckBox)findViewById(R.id.cb_Apple);
        cb_Movie=(CheckBox)findViewById(R.id.cb_Movie);
        cb_TV=(CheckBox)findViewById(R.id.cb_TV);
        btn_Select=(Button)findViewById(R.id.btn_Select);
        btn_Select.setOnClickListener(Check_Select);
    }
    private Button.OnClickListener Check_Select = new Button.OnClickListener(){
        public void onClick(View view){
            String s_Checked="";
            /*接字串符號+=*/
            if(cb_Apple.isChecked()){
                s_Checked+=cb_Apple.getText()+",";
            }
            if(cb_Movie.isChecked()){
                s_Checked+=cb_Movie.getText()+",";
            }
            if(cb_TV.isChecked()){
                s_Checked+=cb_TV.getText()+",";
            }
            setTitle("Checked:"+s_Checked);
        }

    };
}

sample_radio_group.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">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <RadioGroup
            android:id="@+id/rdg_Main"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Apple"
                android:id="@+id/rdg_Apple" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Movie"
                android:id="@+id/rdg_Movie" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TV"
                android:id="@+id/rdg_TV" />
        </RadioGroup>
    </LinearLayout>

</LinearLayout>
SampleRadioGroup.java
package com.example.win7.button_activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * Created by win7 on 2015/2/15.
 */
public class SampleRaidGrop extends Activity {
    /*宣告一個控制項,和三個控制項*/
    private RadioGroup rdg_Main;
    private RadioButton rdg_Apple,rdg_Movie,rdg_TV;
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_radio_group);
        setTitle("這是RadioButton的Activity");
        setupViewComponent();
    }
    private void setupViewComponent(){
        rdg_Main=(RadioGroup)findViewById(R.id.rdg_Main);
        rdg_Apple=(RadioButton)findViewById(R.id.rdg_Apple);
        rdg_Movie=(RadioButton)findViewById(R.id.rdg_Movie);
        rdg_TV=(RadioButton)findViewById(R.id.rdg_TV);
        rdg_Main.setOnCheckedChangeListener(Check_Radio);
    }
    /*這裡特別注意RadipGroup所使用的事件是OnCheckedChangeListener*/
    private RadioGroup.OnCheckedChangeListener Check_Radio = new RadioGroup.OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            if(i==rdg_Apple.getId()){
                setTitle("我的喜好:"+rdg_Apple.getText());
            }
            else if(i==rdg_Movie.getId()){
                setTitle("我的喜好:"+rdg_Movie.getText());
            }
            else if(i==rdg_TV.getId()){
                setTitle("我的喜好:"+rdg_TV.getText());
            }
        }
    };
}

2015年2月14日 星期六

Activity

Activity就是代表一個畫面所有的動作和內容,它也是一個被繼承的母類別,一但繼承了它就可以是一個被執行且啟動的畫面,畫面的切換也是Activity的切換,在Android中Activity的切換分為幾種,一種是單純的切換,一種是帶有資料的切換,還有一種是切換以後還要切換回原來的Activity,以下範例展是單純切換,還有帶資料並且切換原來的Activity。

主要功能
主畫面配置兩個按鈕,兩個按鈕都會切換到新的Activity並且載入新的Layout不同的是一個有登記回呼的方法,有登記回呼的Button可以在Activity結束的時候傳遞資訊回到原本Activity中並且將資訊顯是在畫面上。

重點觀念
開啟一個新的Activity主要有兩種方法,一種是只有傳遞資料與狀態給下一個Activity,另一種還可讓下一個Activity傳遞資料回到原本的Activity,這兩種方法為呼叫startActivity()方法與呼叫startActivityForResult()方法。

startActivity():啟動另一個活動可以說是一去不復返,單純啟動另一個活動

startActivityForResult():是透過覆寫onActivityResult()方法,當這個方法被覆寫了以後,新的Activity呼叫setResult()方法及finish()方法以後就會被回呼,並根據setResult()方法呼叫時所傳遞的參數來接收新的Activity所回傳的資料或資訊,來做後續的動作。


程式碼中建立新的 Activity 類別檔案

其實很簡單需做以下三件事
1.新增一個java (在src/ 下面)  
2.新增一個.xml (在res/layou/ 下面)
3.在AndroidManifest.xml (裡面加入一段敘述)
1.新增java
首先在專案目錄上點右鍵,選擇「New->Class」選項,會出現一個New Java Class的視窗
在Package那爛的右邊Browse點取,選取我們的路徑(一開始我們所創的路徑)
在Name輸入新的檔案名(注意第一個字母需大寫)
接下來在Superclass那欄右手邊的Browse點開
在Choose a type 輸入activity 將會出現Activity-android.app選取 按OK  Finish 。
2.新增.xml
首先在專案目錄上點右鍵,選擇「New->Other->Android->Android XML File」選項
會出現New Android XML File 的視窗
首先選取你的Project  ,File輸入你的檔案名稱(注意後面需+.xml    EX:test.xml)
在下面選取Layout  在點Finish  。
3.AndroidManifest.xml
在我們左邊Navigator(預設的位置)  專案下會有一個名為AndroidManifest.xml 的檔案
雙擊開啟它,在點選下面AndroidManifest.xml模式,下入下面這段敘述
加入:<activity android:name="Activitysample2"></activity>//新增的JAVA檔

activity_main.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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="呼叫 Activity1"
        android:id="@+id/btn_Call_Activity1"
        android:layout_below="@+id/textView"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="呼叫 Activity2"
        android:id="@+id/btn_Call_Activity2"
        android:layout_below="@+id/btn_Call_Activity1"
        android:layout_alignParentStart="true" />

</RelativeLayout>

MainActivity.java

package com.example.win7.helloworld;
/*appMain*/
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


/*繼承Activity類別*/
public class MainActivity extends Activity {
    private  Button btn_Call_Activity1;
    private  Button btn_Call_Activity2;
    static final int REQUEST_CODE=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setupViewComponent();
    }

    private void setupViewComponent(){
        btn_Call_Activity1=(Button)findViewById(R.id.btn_Call_Activity1);
        btn_Call_Activity2=(Button)findViewById(R.id.btn_Call_Activity2);
        btn_Call_Activity1.setOnClickListener(Call_Activity1);
        btn_Call_Activity2.setOnClickListener(Call_Activity2);
    }
    private Button.OnClickListener Call_Activity1=new Button.OnClickListener(){

        @Override
        public void onClick(View view) {
            /*實體化Intent物件,第一個參數載入目前所在的context,第二個參數傳入頁面切換的目的地的class*/
            Intent intent1 = new Intent(MainActivity.this,SampleActivity1.class );
            /*呼叫Intent類別的putExtra方法,第一個字串參數是參數名稱,第二個字串參數是參數值*/
            intent1.putExtra("FromAppMain","appMain");
            /*呼叫Activity的startActivityForResult方法,將前面實體化的Intent物件參考傳入,並且將從Activity繼承而來的REQUEST_CODE變數參考傳入,以記錄一些狀態在返回以後取出使用*/
            startActivityForResult(intent1,REQUEST_CODE);
        }
    };
    private Button.OnClickListener Call_Activity2=new Button.OnClickListener(){

        @Override
        public void onClick(View view) {
           setTitle("目前是由Activity2所返回");
            Intent intent2=new Intent(MainActivity.this,SampleActivity2.class);
            /*切換Activity*/
            startActivity(intent2);
        }
    };
    /*onActivityResult()方法是覆寫用來當新的Activity結束以後被自動回呼,如此一來就可以透過傳遞回來的參數來取得新的Activity,
            要注意的事覆寫onActivityResult()方法的同時,起動新的Activity要使用startActivityForResult()方法,否則就無法自動會呼,
            另外第一個參數用來承載新的Activity的狀態,第二個參數用來承載新的Activity要傳遞給原來的Activity的資料*/
    protected void onActivityResult(int requestCode,int resultCode,Intent date){
        if(requestCode==REQUEST_CODE)
        {
            if(resultCode==RESULT_OK){
                String temp=null;
                /*呼叫Intent的getExtras()方法取得Bundle物件可以用來裝載其他的物件,這些物件經過適當的轉型就可以拿來使用*/
                Bundle extras=date.getExtras();
                if(extras!=null){
                    temp=extras.getString("FromActivity1");
                }
                setTitle(temp);
            }
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
sample_activity1.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">


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent">

        <TextView
            android:layout_width="369dp"
            android:layout_height="112dp"
            android:id="@+id/txv_Activity1" />

        <Button
            android:layout_width="288dp"
            android:layout_height="wrap_content"
            android:text="返回主畫面"
            android:id="@+id/btn_Activity1_BackTo_MainActivity" />
    </LinearLayout>

</LinearLayout>
SampleActivity1.java
package com.example.win7.helloworld;

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

/**
 * Created by win7 on 2015/2/14.
 */
public class SampleActivity1 extends Activity{
        private Button btn_Activity_BackTo_MainActivity;
        private TextView txv_Activity1;
        Bundle bul_Extra;
        String s_ActivityDate=null;
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_activity1);
        setTitle("目前是由Activity1");
        setupViewComponent();
    }
    private void setupViewComponent(){
        txv_Activity1=(TextView)findViewById(R.id.txv_Activity1);
        btn_Activity_BackTo_MainActivity=(Button)findViewById(R.id.btn_Activity1_BackTo_MainActivity);
        btn_Activity_BackTo_MainActivity.setOnClickListener(Activity1);
        /*呼叫getIntent()方法取得用來啟動這個Activity的Intent物件,再對其呼叫getExtras()方法取得其中用來傳遞資料的bundle物件*/
        bul_Extra=getIntent().getExtras();

        if(bul_Extra!=null){
            /*呼叫Bundle的getSting()方法取得放在裡面的字串,並且指派給s_ActivityDate變數*/
            s_ActivityDate=bul_Extra.getString("FromAppMain");
        }
        txv_Activity1.setText("資料是由"+s_ActivityDate+"所傳入");
    }
    private Button.OnClickListener Activity1=new Button.OnClickListener(){

        @Override
        public void onClick(View view) {
            /*實體化Bundle物件,呼叫其putSttring()方法將文字傳入,用Buundle物件作韋Activity切換時候傳遞資訊的載具*/
            Bundle bundle=new Bundle();
            bundle.putString("FromActivity1","目前是由Activity1所返回");
            /*建立一個Intent物件,使用期作為Activity轉換工具物件*/
            Intent mIntent = new Intent();
            /*使用bundle物件來裝載要在Activity間傳遞資料,呼叫putExtra()方法將Bundle物件放入Intent物件中*/
            mIntent.putExtras(bundle);
            /*呼叫setResult()方法,第一個參數傳入目前Activity執行的結果,第二個參數放入要傳回給原來的Activity的Intent物件*/
            setResult(RESULT_OK,mIntent);
            finish();//結束目前的Activity
        }
    };
}

sample_activity2.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">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回主畫面"
        android:id="@+id/btn_Activity2_BackTo_MainActivity" />
</LinearLayout>
SampleActivity2.java
package com.example.win7.helloworld;

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

/**
 * Created by win7 on 2015/2/14.
 */
public class SampleActivity2 extends Activity {
        private Button btn_Activity2_BackTo_MainActivity;
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_activity2);
        setTitle("目前是由Activity2");
        setupViewComponent();
    }

    private void setupViewComponent(){
        btn_Activity2_BackTo_MainActivity=(Button)findViewById(R.id.btn_Activity2_BackTo_MainActivity);
        btn_Activity2_BackTo_MainActivity.setOnClickListener(Activity2);
    }
    private  Button.OnClickListener Activity2=new Button.OnClickListener() {
        @Override
        public void onClick(View view) {
            /*與第一個Activity不同的是這個Activity就直接結束,並沒有處理回呼所要傳遞的資料,也沒有呼叫setResult()方法去設定執行的結果狀態*/
            finish();
        }
    };
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.win7.helloworld" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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=".SampleActivity1"></activity>
        <activity android:name=".SampleActivity2"></activity>
    </application>

</manifest>