1. 透過Bundle物件傳遞參數到下一個Activity

2. 第一個Activity(MainActivity.java)

01.package org.me.android_transferparam;
02.import android.app.Activity;
03.import android.content.Intent;
04.import android.os.Bundle;
05.import android.view.View;
06.import android.widget.Button;
07.import android.widget.EditText;
08. 
09.public class MainActivity extends Activity {
10.    private Button nextButton;
11.    private EditText nameEdit;
12.    private EditText phoneEdit;
13. 
14.    @Override
15.    public void onCreate(Bundle icicle) {
16.        super.onCreate(icicle);
17.        setContentView(R.layout.main);
18.        //載入所有的Widget
19.        loadWidget();
20.         
21.        nextButton.setOnClickListener(new Button.OnClickListener(){
22.           @Override
23.           public void onClick(View view){
24.               //傳送參數到下一個Actitity
25.               deliverToNextActitity();
26.           }
27.        });
28.    }
29. 
30.    public void deliverToNextActitity(){
31.        //建立一個bundle物件,並將要傳遞的參數放到bundle裡
32.        Bundle bundle = new Bundle();
33.        bundle.putString("name", nameEdit.getText().toString());
34.        bundle.putString("phone", phoneEdit.getText().toString());
35.        Intent intent = new Intent();
36.        //設定下一個Actitity
37.        intent.setClass(this, SecondActivity.class);
38.        intent.putExtras(bundle);
39.        //開啟Activity
40.        startActivity(intent);
41.    }
42. 
43.    private void loadWidget(){
44.        nextButton = (Button) findViewById(R.id.nextButton);
45.        nameEdit = (EditText) findViewById(R.id.nameEdit);
46.        phoneEdit = (EditText) findViewById(R.id.phoneEdit);
47.    }
48.}


3. 第二個Activity(SecondActivity.java)

01.package org.me.android_transferparam;
02.import android.app.Activity;
03.import android.os.Bundle;
04.import android.view.View;
05.import android.widget.Button;
06.import android.widget.TextView;
07. 
08.public class SecondActivity extends Activity {
09. 
10.    @Override
11.    public void onCreate(Bundle icicle) {
12.        super.onCreate(icicle);
13.        setContentView(R.layout.second);
14.        TextView content = (TextView) findViewById(R.id.content);
15.        Button backButton = (Button) findViewById(R.id.backButton);
16.        //取得前一個Activity傳過來的Bundle物件
17.        Bundle bundle = getIntent().getExtras();
18.        content.setText("姓名:"+bundle.getString("name")+"   電話:"+bundle.getString("phone"));
19. 
20.        backButton.setOnClickListener(new Button.OnClickListener(){
21.           @Override
22.           public void onClick(View view){
23.               //結束此Activity
24.               finish();
25.           }
26.        });
27.    }
28.}


4. main.xml(Layout)


01.<?xml version="1.0" encoding="utf-8"?>
02.<AbsoluteLayout
03.    android:id="@+id/widget149"
04.    android:layout_width="fill_parent"
05.    android:layout_height="fill_parent"
07.    >
08.    <EditText
09.        android:id="@+id/nameEdit"
10.        android:layout_width="100px"
11.        android:layout_height="36px"
12.        android:layout_x="58px"
13.        android:layout_y="12px"
14.        >
15.    </EditText>
16.    <TextView
17.        android:id="@+id/nameText"
18.        android:layout_width="wrap_content"
19.        android:layout_height="wrap_content"
20.        android:text="姓名:"
21.        android:layout_x="0px"
22.        android:layout_y="18px"
23.        >
24.    </TextView>
25.    <TextView
26.        android:id="@+id/phoneText"
27.        android:layout_width="wrap_content"
28.        android:layout_height="-12px"
29.        android:text="電話:"
30.        android:layout_x="-1px"
31.        android:layout_y="62px"
32.        >
33.    </TextView>
34.    <EditText
35.        android:id="@+id/phoneEdit"
36.        android:layout_width="100px"
37.        android:layout_height="36px"
38.        android:layout_x="58px"
39.        android:layout_y="57px"
40.        >
41.    </EditText>
42.    <Button
43.        android:id="@+id/nextButton"
44.        android:layout_width="wrap_content"
45.        android:layout_height="wrap_content"
46.        android:text="傳送到下一個Activity"
47.        android:layout_x="4px"
48.        android:layout_y="103px"
49.        >
50.    </Button>
51.</AbsoluteLayout>


5. second.xml(Layout)

01.<?xml version="1.0" encoding="UTF-8"?>
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03.    android:orientation="vertical"
04.    android:layout_width="fill_parent"
05.    android:layout_height="fill_parent">"
06.    <TextView
07.        android:id="@+id/content"
08.        android:layout_width="wrap_content"
09.        android:layout_height="wrap_content">
10.    </TextView>
11.    <Button
12.        android:id="@+id/backButton"
13.        android:layout_width="wrap_content"
14.        android:layout_height="wrap_content"
15.        android:text="回前一個Activity">
16.    </Button>
17.</LinearLayout>


6. 程式啟動及輸入參數之後的畫面

 

 

 

 

arrow
arrow
    全站熱搜

    小犬 發表在 痞客邦 留言(0) 人氣()