Android中Activity之間的數據傳遞(Intent和Bundle)

當一個Activity啓動另一個Activity時,常常會有一些數據傳過去,對於Activity之間的數據交換更簡單,因爲兩個Activity之間進行數據傳遞交換更簡單,因爲兩個Activity之間本來就有一個“信使”Intent。

      Intent的用途其實有很多,今天用到的只是最基本的。

      視頻教程:http://v.youku.com/v_show/id_XNzQxMDUxNTU2.html

                       http://www.iqiyi.com/w_19rsgrq0wt.html#vfrm=14-7-2-1

             郵箱:[email protected]



主要是以下方法:

第一種:
          intent = new Intent(MainActivity.this,Second.class);
          bundle = new Bundle();
          bundle.putString("nihao", ed_content.getText().toString());
          intent.putExtras(bundle);
          startActivity(intent);
    接收:
          Intent intent = getIntent();
          Bundle bundle = intent.getExtras();
          tv_show.setText(bundle.getString("nihao"));
 
第二種:
          intent = new Intent();
          intent.setClass(MainActivity.this,Second.class);
          intent.putExtra("key", "dash");
          startActivity(intent); 
    接收:
          Intent intent = getIntent();
          Bundle bundle = intent.getExtras();
          String name = bundle.getString("key");
          tv_show.setText(name);
 
首先對於在API中查到的參數需要注意以下:
context : 這個參數代表了,整個android應用的接口,幾乎所有創造的組件都要用到。一般都是 類名.this


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章