ANDROID中ACTIVITY间的数据传递
效果:有兩個(gè)Activity分別為A和B,從A中采用Bundle封裝數(shù)據(jù)向B中傳遞數(shù)據(jù),然后使用startActivityForResult在B中修改后回傳數(shù)據(jù)。
第一個(gè)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>效果如下:
剛開(kāi)始只是一個(gè)TextView顯示W(wǎng)elcome to my blog:現(xiàn)在需要的是在點(diǎn)擊編輯按鈕時(shí)將TextView里的數(shù)據(jù)傳遞給第二個(gè)Activity編輯,然后回傳編輯后的數(shù)據(jù)。
在第一個(gè)Activity中,采用Bundle封裝并傳遞數(shù)據(jù),從TextView里獲取文本用getText()方法,核心代碼如下
1 text = (TextView)findViewById(R.id.text);2 editBtn = (Button)findViewById(R.id.edit_btn);3 editBtn.setOnClickListener(new OnClickListener(){4 @Override5 public void onClick(View v) {6 // TODO Auto-generated method stub7 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)的主要作用就是它可以回傳數(shù)據(jù),這里需注意的是requestCode要大于等于0,用于回傳時(shí)識(shí)別用的。
好了,數(shù)據(jù)是發(fā)出去了,那怎樣在第二個(gè)Activity接收呢?先開(kāi)布局文件:
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/是我另外加進(jìn)去的,從主Activity傳遞過(guò)來(lái)的數(shù)據(jù)是Welcome to my blog:確定按鈕會(huì)把修改過(guò)的數(shù)據(jù)回傳,取消按鈕則會(huì)返回主Activity然后提示未作任何修改。
首先接收主Activity穿過(guò)來(lái)的數(shù)據(jù):
1 Bundle bundle = getIntent().getExtras(); 2 editText.setText(bundle.getString("text_view"));接下來(lái)確定按鈕將修改過(guò)的數(shù)據(jù)封裝回傳:
1 submit.setOnClickListener(new OnClickListener(){2 @Override3 public void onClick(View v) {4 // TODO Auto-generated method stub5 // 實(shí)例化 Bundle,設(shè)置需要傳遞的參數(shù)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 });點(diǎn)擊取消按鈕
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)將結(jié)果回傳,resultCode用于區(qū)分不同的返回結(jié)果,RESULT_OK和RESULT_CANCELED是自帶的參數(shù),方便使用,在下面還會(huì)提一下,而intent中可以封裝修改后的數(shù)據(jù)傳回給主Activity。還有一點(diǎn)就是,最后要調(diào)用當(dāng)前Activity的finish()方法。
下面在主Activity中重寫(xiě)onActivityResult(int requestCode, int resultCode, Intent data)方法,requestCode和startActivityForResul(Intent?intent, int requestCode)中requestCode的對(duì)應(yīng),resultCode則是上面穿過(guò)來(lái)的值,data是回傳的值。重寫(xiě)方法的代碼為:
1 @Override2 protected void onActivityResult(int requestCode, int resultCode, Intent data) {3 // TODO Auto-generated method stub4 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 }修改文字點(diǎn)擊確定返回后的效果圖如下
點(diǎn)擊取消效果如下
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/xiaoran1129/archive/2012/07/04/2576157.html
總結(jié)
以上是生活随笔為你收集整理的ANDROID中ACTIVITY间的数据传递的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: PowerDesigner pdm生成A
- 下一篇: 自己使用window.open和wind