Activity中的資料傳遞

2021-08-02 15:50:11 字數 1751 閱讀 4913

在android開發中,經常要在activity之間傳遞資料。internet可以用來開啟activity,同樣他也可以用來在activity之間傳遞資料。

在android開發中,經常要在activity之間傳遞資料。internet可以用來開啟activity,同樣他也可以用來在activity之間傳遞資料。使用internet傳遞資料只需要呼叫putextra()方法將想要儲存的資料存在internet中即可。例如,在activity01中儲存乙個字串,現在要將這個字串傳遞到activity02中,可以使用如下**:

string data="hello activity"

intent intent = new intent(this,activity02.class);

intent.putextra("extra_data",data);

staractivity(intent);

在上述**中,通過顯式意圖開啟activity02.並通過putextra()方法傳遞了乙個字串data。putextra()方法中第乙個引數接受的key,第二個引數接收的是value。
如果想要在activity02中取出傳遞過來的資料,可以使用如下**:
intent intent =getintent();
string data==intent.getstringextra("extra_data");
log.i("activiry01",data);
上述這種資料傳遞方法是最簡單的一種資料傳遞方式,還有一種資料傳遞方式是呼叫putextra()方法傳遞資料,該方法傳遞的是bundle物件。呼叫putextra()方法傳遞資料可以用如下**:
bundle bundle-new bundle();
bundle。put string("name","linda");
bundle.putint("age",20);
intent intent-new intent(this,activity01.class);
intent.putextra(bundle);
startactivity(intent);
如果想要在activity01中取出上述方法傳遞的資料,可以使用如下**:
intent intent-getintent();
bundle bundle=intent.getstring("name"):

string stuname-bundele.getstring("name");

int stuage-bundle.getsring("age");
在上述**中,在接收bundle物件封裝的資料時,需要先建立對應的bundle物件,然後再根據資料存入的key值取出value。其實用intent傳遞資料以及物件時,它的內部也呼叫了bundle物件相應的put方法,也就是說intent內部也是用bundle來實現資料傳遞的,只是封裝了一層而已



Activity中的資料傳遞

將activity01中的資料傳輸到activity02中,如下 intent intent new intent this,activity.class 宣告intent物件,intent.putextra extra date hello activity02 用putextra 方法傳遞int...

Activity幾種資料傳遞的方法

4 在任何其他地方呼叫這個方法取出資料 2 剪下板來傳遞資料 1 得到剪下板物件 2 在另乙個activity中得到資料 clipboardmanager cbmanager clipboardmanager getsystemservice context.clipboard service cb...

ifream中的資料傳遞

以下兩種方法都是使用js 的方法!其他部分省略,只寫下關鍵的一句。方法一 parent.document.all.ifr.innerhtml ifream需要傳遞到上級窗體的值 parent 指定上級窗體 document 窗體物件 all 範圍為所有控制項 ifr 某乙個控制項的id 本例的id是...