效果:有兩個Activity分別為A和B,從A中采用Bundle封裝數據向B中傳遞數據,然后使用startActivityForResult在B中修改后回傳數據。
第一個Activity的layout如main.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent"> 6 <TextView 7 android:id="@+id/text" 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="Welcome to my blog:"/> 11 <Button 12 android:id="@+id/edit_btn" 13 android:layout_width="fill_parent" 14 android:layout_height="wrap_content" 15 android:text="編輯"/> 16 </LinearLayout>
效果如下:
剛開始只是一個TextView顯示Welcome to my blog:現在需要的是在點擊編輯按鈕時將TextView里的數據傳遞給第二個Activity編輯,然后回傳編輯后的數據。
在第一個Activity中,采用Bundle封裝並傳遞數據,從TextView里獲取文本用getText()方法,核心代碼如下
1 text = (TextView)findViewById(R.id.text); 2 editBtn = (Button)findViewById(R.id.edit_btn); 3 editBtn.setOnClickListener(new OnClickListener(){ 4 @Override 5 public void onClick(View v) { 6 // TODO Auto-generated method stub 7 Bundle bundle = new Bundle(); 8 bundle.putString("text_view", text.getText().toString()); 9 Intent intent = new Intent(DataTransActivity.this, SecondActivity.class); 10 intent.putExtras(bundle); 11 startActivityForResult(intent, 0); 12 } 13 });
Android中startActivityForResul(Intent intent, int requestCode)的主要作用就是它可以回傳數據,這里需注意的是requestCode要大於等於0,用於回傳時識別用的。
好了,數據是發出去了,那怎樣在第二個Activity接收呢?先開布局文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <EditText 8 android:id="@+id/edittext" 9 android:layout_width="fill_parent" 10 android:layout_height="300dp" /> 11 <LinearLayout 12 android:orientation="horizontal" 13 android:layout_width="fill_parent" 14 android:layout_height="wrap_content"> 15 <Button 16 android:id="@+id/submit" 17 android:layout_width="0dp" 18 android:layout_height="wrap_content" 19 android:layout_weight="1.0" 20 android:text="確定"/> 21 <Button 22 android:id="@+id/cancel" 23 android:layout_width="0dp" 24 android:layout_height="wrap_content" 25 android:layout_weight="1.0" 26 android:text="取消"/> 27 </LinearLayout> 28 </LinearLayout>
效果圖如下,圖中文字http://www.cnblogs.com/sense7/是我另外加進去的,從主Activity傳遞過來的數據是Welcome to my blog:確定按鈕會把修改過的數據回傳,取消按鈕則會返回主Activity然后提示未作任何修改。
首先接收主Activity穿過來的數據:
1 Bundle bundle = getIntent().getExtras(); 2 editText.setText(bundle.getString("text_view"));
接下來確定按鈕將修改過的數據封裝回傳:
1 submit.setOnClickListener(new OnClickListener(){ 2 @Override 3 public void onClick(View v) { 4 // TODO Auto-generated method stub 5 // 實例化 Bundle,設置需要傳遞的參數 6 Bundle bundle = new Bundle(); 7 bundle.putString("edit_text", editText.getText().toString()); 8 SecondActivity.this.setResult(RESULT_OK, SecondActivity.this.getIntent().putExtras(bundle)); 9 SecondActivity.this.finish(); 10 } 11 });
點擊取消按鈕
1 cancel.setOnClickListener(new OnClickListener(){ 2 @Override 3 public void onClick(View v) { 4 // TODO Auto-generated method stub 5 SecondActivity.this.setResult(RESULT_CANCELED); 6 SecondActivity.this.finish(); 7 } 8 });
其中setResut(int resultCode, Intent intent)將結果回傳,resultCode用於區分不同的返回結果,RESULT_OK和RESULT_CANCELED是自帶的參數,方便使用,在下面還會提一下,而intent中可以封裝修改后的數據傳回給主Activity。還有一點就是,最后要調用當前Activity的finish()方法。
下面在主Activity中重寫onActivityResult(int requestCode, int resultCode, Intent data)方法,requestCode和startActivityForResul(Intent intent, int requestCode)中requestCode的對應,resultCode則是上面穿過來的值,data是回傳的值。重寫方法的代碼為:
1 @Override 2 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 3 // TODO Auto-generated method stub 4 super.onActivityResult(requestCode, resultCode, data); 5 if (resultCode == RESULT_OK) { 6 Bundle bundle = data.getExtras(); 7 text.setText(bundle.getString("edit_text").toString()); 8 }else if (resultCode == RESULT_CANCELED) { 9 Toast.makeText(this, "未作任何修改", Toast.LENGTH_SHORT).show(); 10 } 11 }
修改文字點擊確定返回后的效果圖如下
點擊取消效果如下