中文字幕第五页-中文字幕第页-中文字幕韩国-中文字幕最新-国产尤物二区三区在线观看-国产尤物福利视频一区二区

Intent傳遞數(shù)據(jù)

http://blog.csdn.net/sukyle/article/details/4485505

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比灞橋網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式灞橋網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋灞橋地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。

1.Activity之間傳遞數(shù)據(jù)  

(1)利用Intent傳遞數(shù)據(jù)

       傳遞數(shù)據(jù)的Activity中:

      Intent intent = new Intent();
      intent.putExtra("name","Jon");//在Intent中加入鍵值對數(shù)據(jù)。鍵:name,值:Jon
        intent.setClass(Activity01.this,Activity02.class);
        Activity01.this.startActivity(intent);

       在取出數(shù)據(jù)的Activity中:

  Intent intent = getIntent();//獲得傳過來的Intent。
  String value = intent.getStringExtra("name");//根據(jù)鍵name取出值。

(2)利用Bundle傳遞數(shù)據(jù)

傳遞數(shù)據(jù)的Activity:

  Intent intent = new Intent();
  Bundle myBundle = new Bundle();
  myBundle.putString("Key_Name","Tom");
  intent.putExtras(myBundle);
  intent.setClass(Activity01.this,Activity02.class);
  Activity01.this.startActivity(intent);

取出數(shù)據(jù)的Activity:

   Bundle getBundle = getIntent().getExtras();
   String value = getBundle.getString("Key_Name");

(3)利用startActivityForResult傳遞數(shù)據(jù)

startActivityForResult可以把數(shù)據(jù)傳過去,還可以把那邊的數(shù)據(jù)傳過來。

傳遞數(shù)據(jù)的Activity中:

   Intent intent      = new Intent();
   Bundle bundle  = new Bundle();
   bundle.putString("data", "somedata");//把數(shù)據(jù)傳過去
   intent.putExtras(bundle);
   intent.setClass(Activity01.this, Activity02.class);
   startActivityForResult(intent, 10);//10是一個代碼

重載onActivityResult方法,用來接收傳過來的數(shù)據(jù):

 protected void onActivityResult(int requestCode, int resultCode,Intent intent) {
   switch (resultCode) {
   case RESULT_OK:
      Bundle b = intent.getExtras();
      String str = b.getString("Result");
      setTitle("Return data:" + str);
       break;
  default:
        break;
  }
  }

接收數(shù)據(jù)的Activity:

  Intent   intent        = getIntent();
  Bundle getBundle = getIntent().getExtras();
  String   data          = getBundle.getString("data");//讀取傳過來的數(shù)據(jù)
  et.setText(data);

  EditText edittext = (EditText) findViewById(R.id.text);
  Intent intent = new Intent();//實(shí)例化一個Intent用來傳過去,可以在Intent里存放數(shù)據(jù)。
  Bundle bundle = new Bundle();
  bundle.putString("Result",edittext.getText().toString());
  intent.putExtras(bundle);
  Activity02.this.setResult(RESULT_OK,intent);//把Intent(數(shù)據(jù))傳過去,RESULT_OK是請求碼。
  finish();//結(jié)束當(dāng)前的Activity。

 

 

2.http://xqjay19910131-yahoo-cn.iteye.com/blog/1280857

Activity傳遞一個或者多個對象

Activity之間傳遞對象,或者通過Bundle傳遞對象的兩種方式。

1:Serializable方式
  傳遞一個對象

2:Parcelable方式
  傳遞一個對象、傳遞多個對象(ArrayList<Object>)


方式一:Serializable

     傳遞類:
    

Java代碼 Intent傳遞數(shù)據(jù) Intent傳遞數(shù)據(jù)Intent傳遞數(shù)據(jù)
  1. public class CustomeClass implements Serializable{   
  2.        
  3.     /** 
  4.      *  
  5.      */  
  6.     private static final long serialVersionUID = -7060210544600464481L;   
  7.     private String name;   
  8.     private String id;   
  9.     private int age;   
  10.     private String sex;   
  11.        
  12.     public String getName() {   
  13.         return name;   
  14.     }   
  15.     public void setName(String name) {   
  16.         this.name = name;   
  17.     }   
  18.     public String getId() {   
  19.         return id;   
  20.     }   
  21.     public void setId(String id) {   
  22.         this.id = id;   
  23.     }   
  24.     public int getAge() {   
  25.         return age;   
  26.     }   
  27.     public void setAge(int age) {   
  28.         this.age = age;   
  29.     }   
  30.     public String getSex() {   
  31.         return sex;   
  32.     }   
  33.     public void setSex(String sex) {   
  34.         this.sex = sex;   
  35.     }   
  36.   
  37. }  
public class CustomeClass implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7060210544600464481L;
	private String name;
	private String id;
	private int age;
	private String sex;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}

}




       發(fā)送部分:
      

Java代碼 Intent傳遞數(shù)據(jù) Intent傳遞數(shù)據(jù)Intent傳遞數(shù)據(jù)
  1. CustomeClass cc = new CustomeClass();   
  2. cc.setAge(21);   
  3. cc.setId("123456");   
  4. cc.setName("mingkg21");   
  5. cc.setSex("男");   
  6.   
  7. Intent intent = new Intent(this, PersonInfo.class);   
  8. intent.putExtra("PERSON_INFO", cc);   
  9. startActivity(intent);  
        CustomeClass cc = new CustomeClass();
        cc.setAge(21);
        cc.setId("123456");
        cc.setName("mingkg21");
        cc.setSex("男");
        
        Intent intent = new Intent(this, PersonInfo.class);
        intent.putExtra("PERSON_INFO", cc);
        startActivity(intent);



       
       接收部分:
      

Java代碼 Intent傳遞數(shù)據(jù) Intent傳遞數(shù)據(jù)Intent傳遞數(shù)據(jù)
  1.        Intent intent = getIntent();   
  2. CustomeClass cc = CustomeClass)intent.getSerializableExtra("PERSON_INFO");   
  3. setTextView(R.id.id, cc.getId());   
  4. setTextView(R.id.name, cc.getName());   
  5. setTextView(R.id.sex, cc.getSex());   
  6. setTextView(R.id.age, String.valueOf(cc.getAge()));  
        Intent intent = getIntent();
	CustomeClass cc = CustomeClass)intent.getSerializableExtra("PERSON_INFO");
	setTextView(R.id.id, cc.getId());
	setTextView(R.id.name, cc.getName());
	setTextView(R.id.sex, cc.getSex());
	setTextView(R.id.age, String.valueOf(cc.getAge()));




方式二:Parcelable

     傳遞類:
    

Java代碼 Intent傳遞數(shù)據(jù) Intent傳遞數(shù)據(jù)Intent傳遞數(shù)據(jù)
  1. public class CustomeParcelable implements Parcelable {   
  2.        
  3.     private String name;   
  4.     private String id;   
  5.     private int age;   
  6.     private String sex;   
  7.   
  8.     public String getName() {   
  9.         return name;   
  10.     }   
  11.   
  12.     public void setName(String name) {   
  13.         this.name = name;   
  14.     }   
  15.   
  16.     public String getId() {   
  17.         return id;   
  18.     }   
  19.   
  20.     public void setId(String id) {   
  21.         this.id = id;   
  22.     }   
  23.   
  24.     public int getAge() {   
  25.         return age;   
  26.     }   
  27.   
  28.     public void setAge(int age) {   
  29.         this.age = age;   
  30.     }   
  31.   
  32.     public String getSex() {   
  33.         return sex;   
  34.     }   
  35.   
  36.     public void setSex(String sex) {   
  37.         this.sex = sex;   
  38.     }   
  39.        
  40.     public static final Parcelable.Creator<CustomeParcelable> CREATOR = new Creator<CustomeParcelable>(){   
  41.   
  42.         public CustomeParcelable createFromParcel(Parcel source) {   
  43.             // TODO Auto-generated method stub  
  44.             CustomeParcelable cus = new CustomeParcelable();   
  45.             cus.name = source.readString();   
  46.             cus.id = source.readString();   
  47.             cus.age = source.readInt();   
  48.             cus.sex = source.readString();   
  49.             return cus;   
  50.         }   
  51.   
  52.         public CustomeParcelable[] newArray(int size) {   
  53.             // TODO Auto-generated method stub  
  54.             return new CustomeParcelable[size];   
  55.         }   
  56.            
  57.     };   
  58.   
  59.     public int describeContents() {   
  60.         // TODO Auto-generated method stub  
  61.         return 0;   
  62.     }   
  63.   
  64.     public void writeToParcel(Parcel dest, int flags) {   
  65.         // TODO Auto-generated method stub  
  66.         dest.writeString(name);   
  67.         dest.writeString(id);   
  68.         dest.writeInt(age);   
  69.         dest.writeString(sex);   
  70.     }   
  71.   
  72. }  
public class CustomeParcelable implements Parcelable {
	
	private String name;
	private String id;
	private int age;
	private String sex;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
	
	public static final Parcelable.Creator<CustomeParcelable> CREATOR = new Creator<CustomeParcelable>(){

		public CustomeParcelable createFromParcel(Parcel source) {
			// TODO Auto-generated method stub
			CustomeParcelable cus = new CustomeParcelable();
			cus.name = source.readString();
			cus.id = source.readString();
			cus.age = source.readInt();
			cus.sex = source.readString();
			return cus;
		}

		public CustomeParcelable[] newArray(int size) {
			// TODO Auto-generated method stub
			return new CustomeParcelable[size];
		}
		
	};

	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		dest.writeString(name);
		dest.writeString(id);
		dest.writeInt(age);
		dest.writeString(sex);
	}

}



       發(fā)送部分:
      

Java代碼 Intent傳遞數(shù)據(jù) Intent傳遞數(shù)據(jù)Intent傳遞數(shù)據(jù)
  1. CustomeParcelable cc = new CustomeParcelable();   
  2. cc.setAge(21);   
  3. cc.setId("123456");   
  4. cc.setName("mingkg21");   
  5. cc.setSex("男");   
  6.            
  7. Intent intent = new Intent(this, PersonInfo.class);   
  8. intent.putExtra("PERSON_INFO", cc);   
  9. startActivity(intent);  
CustomeParcelable cc = new CustomeParcelable();
cc.setAge(21);
cc.setId("123456");
cc.setName("mingkg21");
cc.setSex("男");
        
Intent intent = new Intent(this, PersonInfo.class);
intent.putExtra("PERSON_INFO", cc);
startActivity(intent);



        接受部分:
       

Java代碼 Intent傳遞數(shù)據(jù) Intent傳遞數(shù)據(jù)Intent傳遞數(shù)據(jù)
  1. Intent intent = getIntent();   
  2. CustomeParcelable cc = intent.getParcelableExtra("PERSON_INFO");   
  3. setTextView(R.id.id, cc.getId());   
  4. setTextView(R.id.name, cc.getName());   
  5. setTextView(R.id.sex, cc.getSex());   
  6. setTextView(R.id.age, String.valueOf(cc.getAge()));  
Intent intent = getIntent();
CustomeParcelable cc = intent.getParcelableExtra("PERSON_INFO");
setTextView(R.id.id, cc.getId());
setTextView(R.id.name, cc.getName());
setTextView(R.id.sex, cc.getSex());
setTextView(R.id.age, String.valueOf(cc.getAge()));




以上為Parcelable傳遞一個對象,若要實(shí)現(xiàn)傳遞多個對象,
傳遞部分:
 

Java代碼 Intent傳遞數(shù)據(jù) Intent傳遞數(shù)據(jù)Intent傳遞數(shù)據(jù)
  1. Bundle bundle = new Bundle();   
  2.         bundle.putParcelableArrayList("mP3TagForNetDTOs",mP3TagForNetDTOs);   
  3.         msg.setData(bundle);   
  4.         endDocNotice.sendMessage(msg);  
Bundle bundle = new Bundle();
		bundle.putParcelableArrayList("mP3TagForNetDTOs",mP3TagForNetDTOs);
		msg.setData(bundle);
		endDocNotice.sendMessage(msg);



接受部分:
 

Java代碼  
  1. Bundle bundle =  msg.getData();   
  2.                 mP3TagForNetDTOs = bundle.getParcelableArrayList("mP3TagForNetDTOs");  



 

文章題目:Intent傳遞數(shù)據(jù)
文章轉(zhuǎn)載:http://www.2m8n56k.cn/article18/poocgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站用戶體驗(yàn)做網(wǎng)站定制網(wǎng)站網(wǎng)站策劃商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
主站蜘蛛池模板: 亚洲美女视频 | 国产欧美一区二区三区视频 | 亚州视频一区二区 | 久久全国免费久久青青小草 | 久久精品一区二区免费看 | 国产色手机在线观看播放 | 日本欧美大片 | 日韩专区欧美 | 色情毛片| 国产精品久久久久一区二区 | 日韩欧美中文字幕一区二区三区 | 加勒比色 | 黄男人和女人色一级 | 欧美在线成人免费国产 | 国产一级做a爰片在线看免费 | 91不卡在线精品国产 | 精品真实国产乱文在线 | 欧美亚洲国产精品 | a级国产 | 91精品国产免费网站 | 久久精品免费一区二区视 | 亚洲精品第一第二区 | 一级真人毛片 | 精品国产自在在线在线观看 | 日韩美女视频一区 | 中文字幕一区二区在线观看 | 亚洲精品视频免费观看 | 成年人黄国产 | 国产一区二区在线视频播放 | 国产高清在线观看 | 欧美三级真做在线观看 | 久久精品视频9 | 美国a毛片 | 黄色网址免费在线 | a级国产乱理伦片在线观看国 | xxx欧美老熟 | 午夜免费成人 | 国产成人在线观看免费网站 | 亚洲免费网址 | 玖草在线 | www.日本在线观看 |