//主頁面主要代碼: // 選擇圖片按鈕事件 public void choose(View view) { Intent intent = new Intent(this, NextActivity.class); startActivityForResult(intent, REQUESTCODE); // 跳轉下一頁的請求碼REQUESTCODE=100,用帶回數據的跳轉,重寫onActivityResult()方法 } // 數據返回後將圖片顯示在imageView protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUESTCODE && resultCode == 101) { // 判斷請求碼和結果碼是否相同,便是否是對應的Intent Bundle bundle = data.getExtras(); int headerid = bundle.getInt("headerId"); // headerId注意這裏獲取的鍵要與傳過來的鍵值對應,不然會出錯 imageView.setImageResource(headerid); setTitle(getResources().getText(headerid));// 在頭部顯示資源路徑名 } } //第二個Activity的代碼 public class NextActivity extends Activity { private GridView gridView; private SimpleAdapter adapter; // 圖片數組 private int[] image = { R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04, R.drawable.img05, R.drawable.img06, R.drawable.img07, R.drawable.img08, R.drawable.img09 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.girdview_activity); gridView = (GridView) this.findViewById(R.id.gridview); List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); for (int i = 0; i < image.length; i++) { Map<String, Object> itemMap = new HashMap<String, Object>(); itemMap.put("header", image[i]); data.add(itemMap); } adapter = new SimpleAdapter(this, data, R.layout.girdview_item, new String[] { "header" }, new int[] { R.id.item_imageview }); gridView.setAdapter(adapter); // 點擊選擇圖片監聽 gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { int headerId = image[position]; // 獲取intent,並返回給主頁面數據 Intent intent = getIntent(); Bundle bundle = new Bundle(); bundle.putInt("headerId", headerId); intent.putExtras(bundle); setResult(101, intent); finish();// 關閉該Activity } }); } } //主頁面佈局: <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> <Button android:id="@+id/choose" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="選擇頭像" android:onClick="choose" android:layout_below="@+id/imageview" /> //第二個頁面佈局 <GridView android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="wrap_content" android:numColumns="3" /> //SimpleAdapter適配器自定義佈局 <ImageView android:id="@+id/item_imageview" android:layout_width="match_parent" android:layout_height="wrap_content" />