Android實現(xiàn)帶頭像的用戶注冊頁面
1.首先是注冊頁面的布局:
<?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="確認(rèn)密碼:" /> <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>
效果如下圖所示:

2.然后是圖庫的頁面布局,由用戶去選擇圖片,這里我就用windows系統(tǒng)里面的幾張照片:
<?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>

3.然后我們在注冊頁面的Activity寫入以下代碼:
點擊按鈕跳轉(zhuǎn)到圖庫Activity頁面中:
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);
}
}

4.在圖庫Activity里面寫入以下代碼響應(yīng)用戶點擊圖片并通過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();
}
});
這里主要是重寫getView方法:

5.然后我們點擊運行界面如下:

6.我們點擊按鈕之后跳轉(zhuǎn)Activity選擇圖片頭像:

7.然后我們點擊一個圖片又跳轉(zhuǎn)回去,頭像更改:

相關(guān)文章
利用源碼編譯Android系統(tǒng)的APK和可執(zhí)行命令的方法
這篇文章主要介紹了利用源碼編譯Android系統(tǒng)的APK和可執(zhí)行命令的方法,示例在Linux系統(tǒng)環(huán)境上進行構(gòu)建,需要的朋友可以參考下2016-02-02
Android應(yīng)用開發(fā)中Fragment的靜態(tài)加載與動態(tài)加載實例
這篇文章主要介紹了Android應(yīng)用開發(fā)中Fragment的靜態(tài)加載與動態(tài)加載實例,例子中包括動態(tài)的添加更新以及刪除Fragment等操作,很有借鑒意義,需要的朋友可以參考下2016-02-02
Android 照片選擇區(qū)域功能實現(xiàn)示例
這篇文章主要介紹了Android 照片選擇區(qū)域功能實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Android Mms之:聯(lián)系人管理的應(yīng)用分析
本篇文章是對Android中的聯(lián)系人管理進行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android的HTTP類庫Volley入門學(xué)習(xí)教程
這篇文章主要介紹了Android應(yīng)用開發(fā)框架Volley的入門學(xué)習(xí)教程,Volley適合于輕量級的通信功能開發(fā),善于處理JSON對象,需要的朋友可以參考下2016-02-02
Android 布局中的android:onClick的使用方法總結(jié)
這篇文章主要介紹了Android 布局中的android:onClick的使用方法總結(jié)的相關(guān)資料,設(shè)置點擊時從上下文中調(diào)用指定的方法,這里提供實例幫助大家理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08
ubuntu用wifi連接android調(diào)試程序的步驟
這篇文章主要介紹了ubuntu用wifi連接android調(diào)試程序的步驟,需要的朋友可以參考下2014-02-02
Android中 TeaScreenPopupWindow多類型篩選彈框功能的實例代碼
這篇文章主要介紹了Android TeaScreenPopupWindow多類型篩選彈框功能,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-06-06

