Android程式開發基礎之——頁面傳值

來源:互聯網
上載者:User

Activity跳轉與傳值,主要是通過Intent類來串連多個Activity,通過Bundle類來傳遞資料。

範例程式碼如下:

1.1、使用intent.putExtra()方法賦值

1 public class menu extends Activity {

2

3 @Override

4 public void onCreate(Bundle savedInstanceState) {

5 super.onCreate(savedInstanceState);

6

7 setContentView(R.layout.menu);

8

9 ...

10

11 // Button2

12 Button Btn2 = (Button) findViewById(R.id.button2);

13

14 Btn2.setOnClickListener(new OnClickListener() {

15

16 @Override

17 public void onClick(View arg0) {

18

19 Intent intent = new Intent(menu.this, detail.class);

20 intent.putExtra("flg", "list");

21 startActivity(intent);

22 }

23 });

24

25 ...

26

27

28 }

1.2、在另一個頁面擷取傳值

1 Bundle bundle = this.getIntent().getExtras();

2 String flg = bundle.getString("flg");

3 if (flg.equals("list")) {

4 ...

5 }

2.1、如果要傳遞的是一組資料的話,就要藉助Bundle對象來傳遞資料了。

1 Bundle bundle = new Bundle();

2

3 bundle.putString("Name", "Jacker");

4 bundle.putString("Phone", "13590010101");

5 bundle.putBoolean("flg", true);

6

7 intent.putExtras(bundle);

2.2擷取傳值的方法相同

1 Bundle bundle = this.getIntent().getExtras();

2

3 String name = bundle.getString("Name");

4 String phone = bundle.getString("Phone");

5 boolean flg = bundle.getBoolean("flg");

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.