日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ANDROID中ACTIVITY间的数据传递

發布時間:2024/4/17 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ANDROID中ACTIVITY间的数据传递 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

效果:有兩個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 @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)的主要作用就是它可以回傳數據,這里需注意的是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 @Override3 public void onClick(View v) {4 // TODO Auto-generated method stub5 // 實例化 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 @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 }

修改文字點擊確定返回后的效果圖如下

點擊取消效果如下

?

?

轉載于:https://www.cnblogs.com/xiaoran1129/archive/2012/07/04/2576157.html

總結

以上是生活随笔為你收集整理的ANDROID中ACTIVITY间的数据传递的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。