bundle傳值相關技術

2021-09-23 13:23:02 字數 1702 閱讀 8845

intent.putextra("hello","您好")//鍵值對
接受頁面拿到intent

intent intent = getintent();

string hello = getstringextra(「hello」);

textview tv = new textview;

tv.settext(hello);

bundle

bundle主要用於傳遞資料,它儲存的資料,是以key-value(鍵值對)的形式存在的。

bundle經常使用在activity之間或者執行緒之間傳遞資料,傳遞的資料可以是boolean,byte,int ,long,float,double,string等基本資料型別或它們對應的陣列,也可以是物件或物件陣列。

當bundle傳遞的是物件或物件陣列時,必須實現serializable或parcelable介面。

bundle提供了各種常用型別的put***()/get***方法,用於讀寫基本型別的資料。

在activity間傳遞資訊

bundle bundle = new bundle(); //得到bundle物件

bundle.putstring("sff","value"值); //key-"sff",通過得到value-"value"值(string型)

bundle.putint("iff","175"); //key-"iff",value-175

intent.putintextra(bundle); //通過intent將bundle傳遞給另乙個activity

startactivity(intent);

讀取資料

bundle bundle = this.getintent().getextra(); //讀取intent的資料給bundle物件

string str1 = bundle.getstring("sff"); // 通過key得到value

int int1 = bundle.getint("iff");

執行緒間傳遞

通過handle將帶有bundle資料的message放入訊息佇列,其他執行緒就可以從佇列中得到資料。

message message = new message(); //new乙個message物件

message.what = message_what_2; //給訊息做標記

bundle bundle = new bundle(); //得到bundle物件

bundle.putstring("text1","訊息傳遞引數的列子!") // 往bundle中存放資料

bundle.putint("text2",44); //往bundle中put資料

message.setdata(bundle); //mes利用bundle傳遞資料

mhandler.sendmessage(message); //handle將訊息放入訊息佇列

讀取資料

這裡用的handler的handlemessage(message msg);方法處理資料

string str1 = msg.getdata().getstring("text1");

int int = msg.getdata().getstring("text2");

使用Bundle在Activity之間傳遞資料

bundle可能過put 方法新增各種型別的資料,intent也可以通過putextras bundle 將資料新增進去,然後通過startactivity 跳到下一下activity的時候就把資料也傳到下乙個activity了。package com.intent import android.c...

Bundle類用於傳遞值

android中bundle類的作用 bundle類用作攜帶資料,它類似於 map,用於存放 key value 名值對形式的值。相對於 map,它提供了各種常用型別的 put get 方法,如 putstring getstring 和putint getint put 用於往bundle 物件放...

JS checkbox 全選及傳值相關

判斷checkbox是否被選中 if checkbox id attr checked if checkbox id get 0 checked if checkbox id is checked 前台傳值,後台接收值 string str1 request 引數1 string str2 requ...