Android中Activity之間的通信和傳值

Android 中通過 Intent 對象來表示一條消息,一個 Intent 對象不僅包含有這個消息的目的地,還可以包含消息的內容,通過 Activity. startActivity(intent) 啓動另外一個 Activity ,Intent當作目的地址,Bundle當作所攜帶的內容。

intent直接跳轉啓動到新Activity,也可以帶參數。

 btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
			//第1中方式.打開新的Activity,不傳遞參數
	                //新建一個顯式意圖,第一個參數爲當前Activity類對象,第二個參數爲你要打開的Activity類
			//Intent intent1=new Intent(MainActivity.this,StartActivityForResultActivity.class);
			//startActivity(intent1);

			//第2中方式.打開新的Activity,並傳遞若干個參數給它
		      //Intent當作目的地址,Bundle當作所攜帶的內容
		      Intent intent2=new Intent(MainActivity.this,StartActivityForResultActivity.class);
		      Bundle bundle=new Bundle();
		      bundle.putBoolean("boolean_key", false);
		      bundle.putString("String_key", "apple");
		      bundle.putDouble("Double_key", 6.66);
		      bundle.putInt("int_key", 20);		
		      intent2.putExtra("all_key", bundle);
	              startActivity(intent2);
					
				}
			});

在啓動新的Activity中,取傳過來的數值。

Intent intent = getIntent();
		Bundle bunble = intent.getBundleExtra("all_key");
		boolean booleanValue = bunble.getBoolean("");
		double doubleValue = bunble.getDouble("Double_key");
		String stringValue = bunble.getString("String_key");
		int intValue = bunble.getInt("int_key");
		Log.i("info", "booleanValue"+booleanValue);
		Log.i("info", "doubleValue"+doubleValue);
		Log.i("info", "stringValue"+stringValue);
		Log.i("info", "intValue"+intValue);

打印結果:



通過startActivityForResult(intent, REQUSET)傳值啓動Activity並返回結果:

1.創建Intent對象。

2.啓動指定Activity並等待返回結果,其實請求碼是用來標誌該請求startActivityForResult(intent, REQUSET)。

3.重新onActivityResult(int requestCode, int resultCode, Intent data)接收指定Activity返回的結果。

在指定Activity中:

1.獲取Intent的對象intent。

2.通過intent添加參數,或用Bundle包裝需要返回的數據並put入intnet中。

3.設置該Activity的結果碼,並設置結束之後要返回的Activity: setResult(RESULT_OK, intent)。

4.finish()掉該Activity。

                   

              圖a                                                圖b                                                           圖c

圖a中在StartActivityForResultActivity界面啓動--->進入圖b,在SetResultActivity界面,輸入數值----又返回StartActivityForResultActivity界面,拿到返回的數值,進行顯示。

示例代碼

StartActivityForResultActivity:

 btn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent=new Intent(StartActivityForResultActivity.this,SetResultActivity.class);
					//發送意圖標  REQUSET=1用來標誌請求一般設置int就行
					startActivityForResult(intent, REQUSET);
				}
			});

onActivityResult方法中結束下面代碼傳過來的數據。

若有多個返回請求code,就在下面採用switch分支語句接受不同的請求碼,來進行相應的出來

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		 //requestCode標示請求的標示   resultCode表示有數據  
		if(requestCode==StartActivityForResultActivity.REQUSET&&resultCode==RESULT_OK)
		{
			String value = data.getStringExtra("key_info");
			tv.setText("返回值:"+value);
			Log.i("info", "requestCode="+requestCode+";"+"resultCode="+resultCode);
//			finish();
		}
		 Toast.makeText(  
	                this,  
	                "requestCode=" + requestCode + ":" + "resultCode=" + resultCode,  
	                Toast.LENGTH_LONG).show();  
	}

SetResultActivity:

btn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					 Intent intent=new Intent(); 
					 intent.putExtra("key_info", et.getText().toString()); 
					 setResult(RESULT_OK, intent);
					 finish();
				}
			});


示例代碼


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