詳細的圖文能夠到個人百度經驗去查看:http://jingyan.baidu.com/article/cd4c2979eda109756e6e60de.htmlhtml
首先是註冊頁面的佈局:java
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingTop="20px" android:orientation="horizontal" > <LinearLayout android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_weight="2" android:paddingLeft="20px" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TableLayout android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" > <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:textSize="20px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用戶名:" /> <EditText android:id="@+id/user" android:minWidth="400px" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView2" android:textSize="20px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密碼:" /> <EditText android:id="@+id/pwd" android:inputType="textPassword" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </TableRow> <TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView3" android:textSize="20px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="確認密碼:" /> <EditText android:id="@+id/repwd" android:inputType="textPassword" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </TableRow> <TableRow android:id="@+id/tableRow4" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView4" android:textSize="20px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="E-mail地址:" /> <EditText android:id="@+id/email" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </TableRow> </TableLayout> </LinearLayout> <LinearLayout android:id="@+id/linearLayout2" android:orientation="vertical" android:gravity="center_horizontal" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" > <ImageView android:id="@+id/imageView1" android:layout_width="158px" android:layout_height="150px" android:src="@drawable/ic_launcher" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="選擇頭像" /> </LinearLayout> </LinearLayout>
而後是圖庫的頁面佈局,由用戶去選擇圖片,這裏我就用windows系統裏面的幾張照片android
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <GridView android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/gridView" android:numColumns="4" /> </LinearLayout>
而後咱們在註冊頁面的Activity寫入如下代碼:windows
Button button1=(Button)findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,HeadActivity.class); startActivityForResult(intent,0x11); } }); @Override onActivityResult方法: protected void onActivityResult(int requestCode,int resultCode,Intent data){ super.onActivityResult(requestCode,resultCode,data); if(requestCode==0x11&&requestCode==0x11){ Bundle bundle=data.getExtras(); int imageId=bundle.getInt("imageId"); ImageView imageView=(ImageView)findViewById(R.id.imageView1); imageView.setImageResource(imageId); } }
點擊按鈕跳轉到圖庫Activity頁面中。ide
在圖庫Activity裏面寫入如下代碼響應用戶點擊圖片並經過Intent傳遞給前一個Activity:佈局
GridView gridView=(GridView)findViewById(R.id.gridView); BaseAdapter adapter=new BaseAdapter() { @Override public int getCount() { return imageId.length; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if(convertView==null){ imageView=new ImageView(HeadActivity.this); imageView.setAdjustViewBounds(true); imageView.setMaxHeight(58); imageView.setMaxWidth(50); imageView.setPadding(5,5,5,5); }else{ imageView=(ImageView)convertView; } imageView.setImageResource(imageId[position]); return imageView; } }; gridView.setAdapter(adapter); gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent=getIntent(); Bundle bundle=new Bundle(); bundle.putInt("imageId",imageId[position]); intent.putExtras(bundle); setResult(0x11,intent); finish(); } });
結果以下:this