听闻少年二字,当与平庸相斥。这篇文章主要讲述Android ImageView点击更换头像相关的知识,希望能为你提供帮助。
文章图片
【Android ImageView点击更换头像】
首先搭建布局
主界面布局:
1 < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2android:layout_width="match_parent" 3android:layout_height="match_parent" 4android:orientation="horizontal" > 5 6< LinearLayout 7android:id="@+id/ll" 8android:layout_width="wrap_content" 9android:layout_height="wrap_content" 10android:layout_alignParentRight="true" 11android:gravity="center" 12android:orientation="vertical" > 13 14< ImageView 15android:id="@+id/image_btn" 16android:layout_width="wrap_content" 17android:layout_height="wrap_content" 18android:background="@null" 19android:src="https://www.songbingjia.com/android/@drawable/blank" /> 20 21< Button 22android:id="@+id/select_btn" 23android:layout_width="wrap_content" 24android:layout_height="wrap_content" 25android:layout_below="@id/image_btn" 26android:layout_centerHorizontal="true" 27android:text="选择头像" /> 28< /LinearLayout> 29 30< TableLayout 31android:layout_width="wrap_content" 32android:layout_height="wrap_content" 33android:layout_toLeftOf="@id/ll" 34android:stretchColumns="1" > 35 36< TableRow> 37 38< TextView android:text="用户名:" /> 39 40< EditText /> 41< /TableRow> 42 43< TableRow> 44 45< TextView android:text="密码:" /> 46 47< EditText /> 48< /TableRow> 49 50< TableRow> 51 52< TextView android:text="确认密码:" /> 53 54< EditText /> 55< /TableRow> 56 57< TableRow> 58 59< TextView android:text="E-mail地址:" /> 60 61< EditText /> 62< /TableRow> 63< /TableLayout> 64 65 < /RelativeLayout>
DialogActivity布局
1 < GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 2android:layout_width="match_parent" 3android:layout_height="match_parent" 4android:columnCount="4" 5android:orientation="horizontal" > 6 7< ImageView 8android:id="@+id/image_01" 9android:layout_width="wrap_content" 10android:layout_height="wrap_content" 11android:background="@null" 12android:src="https://www.songbingjia.com/android/@drawable/img01" /> 13 14< ImageView 15android:id="@+id/image_02" 16android:layout_width="wrap_content" 17android:layout_height="wrap_content" 18android:background="@null" 19android:src="https://www.songbingjia.com/android/@drawable/img02" /> 20 21< ImageView 22android:id="@+id/image_03" 23android:layout_width="wrap_content" 24android:layout_height="wrap_content" 25android:background="@null" 26android:src="https://www.songbingjia.com/android/@drawable/img03" /> 27 28< ImageView 29android:id="@+id/image_04" 30android:layout_width="wrap_content" 31android:layout_height="wrap_content" 32android:background="@null" 33android:src="https://www.songbingjia.com/android/@drawable/img04" /> 34 35< ImageView 36android:id="@+id/image_05" 37android:layout_width="wrap_content" 38android:layout_height="wrap_content" 39android:background="@null" 40android:src="https://www.songbingjia.com/android/@drawable/img05" /> 41 42< ImageView 43android:id="@+id/image_06" 44android:layout_width="wrap_content" 45android:layout_height="wrap_content" 46android:background="@null" 47android:src="https://www.songbingjia.com/android/@drawable/img06" /> 48 49< ImageView 50android:id="@+id/image_07" 51android:layout_width="wrap_content" 52android:layout_height="wrap_content" 53android:background="@null" 54android:src="https://www.songbingjia.com/android/@drawable/img07" /> 55 56< ImageView 57android:id="@+id/image_08" 58android:layout_width="wrap_content" 59android:layout_height="wrap_content" 60android:background="@null" 61android:src="https://www.songbingjia.com/android/@drawable/img08" /> 62 63< ImageView 64android:id="@+id/image_09" 65android:layout_width="wrap_content" 66android:layout_height="wrap_content" 67android:background="@null" 68android:src="https://www.songbingjia.com/android/@drawable/img09" /> 69 70 < /GridLayout>
AndroidManifest.xml中注册活动
1< application 2android:allowBackup="true" 3android:icon="@drawable/ic_launcher" 4android:label="@string/app_name" 5android:theme="@style/AppTheme" > 6< activity 7android:name=".MainActivity" 8android:label="@string/app_name" > 9< intent-filter> 10< action android:name="android.intent.action.MAIN" /> 11 12< category android:name="android.intent.category.LAUNCHER" /> 13< /intent-filter> 14< /activity> 15< activity 16android:name=".DialogActivity" 17android:label="选择头像" 18android:theme="@android:style/Theme.Dialog"> 19 20< /activity> 21< /application>
MainActivity主活动加载布局,点击事件,接收返回的结果
1 import android.content.Intent; 2 import android.os.Bundle; 3 import android.view.View; 4 import android.view.View.OnClickListener; 5 import android.widget.Button; 6 import android.widget.ImageView; 7 8 public class MainActivity extends LifeCycleActivity { 9 10Button select; 11ImageView showPic; 12 13public static final int SELECT_PIC = 1; 14 15@Override 16protected void onCreate(Bundle savedInstanceState) { 17super.onCreate(savedInstanceState); 18setContentView(R.layout.activity_main); 19 20select = (Button) findViewById(R.id.select_btn); 21showPic = (ImageView) findViewById(R.id.image_btn); 22 23select.setOnClickListener(new OnClickListener() { 24@Override 25public void onClick(View v) { 26Intent intent = new Intent(MainActivity.this, 27DialogActivity.class); 28startActivityForResult(intent, SELECT_PIC); 29} 30}); 31 32} 33@Override 34protected void onActivityResult(int requestCode, int resultCode, Intent data) { 35if (requestCode == SELECT_PIC & & resultCode == RESULT_OK) { 36int imgid = data.getIntExtra("image", -1); 37//更换图片 38if(imgid!=-1){ 39showPic.setImageResource(imgid); 40} 41} 42} 43 44 } 45 46
DialogActivity活动加载布局,返回数据
这里使用两种方式,一种是使用数据,一种是使用反射
1 import android.content.Intent; 2 import android.os.Bundle; 3 import android.view.View; 4 import android.view.View.OnClickListener; 5 import android.widget.ImageView; 6 7 public class DialogActivity extends LifeCycleActivity { 8 9ImageView[] iv = new ImageView[9]; 10 11 //int[] ids = { R.id.image_01, R.id.image_02, R.id.image_03, R.id.image_04, 12 //R.id.image_05, R.id.image_06, R.id.image_07, R.id.image_08, 13 //R.id.image_09 }; 14 //int[] imgId = { R.drawable.img01, R.drawable.img02, R.drawable.img03, 15 //R.drawable.img04, R.drawable.img05, R.drawable.img06, 16 //R.drawable.img07, R.drawable.img08, R.drawable.img09 }; 17 18@Override 19protected void onCreate(Bundle savedInstanceState) { 20super.onCreate(savedInstanceState); 21setContentView(R.layout.activity_dialog); 22 23//使用反射 24for (int i = 0; i < iv.length; i++) { 25final int finalI = i+1; 26String name = "image_0"+finalI; 27try { 28//获取其他类 29Class cls = R.id.class; 30//获取类的属性,getField(name)获取属性,getInt(null)获取属性对应的值 31//null代表的是静态变量,非静态变量需要传递对象 32int id = cls.getField(name).getInt(null); 33iv[i] = (ImageView) findViewById(id); 34iv[i].setOnClickListener(new OnClickListener() { 35@Override 36public void onClick(View v) { 37 38//返回数据 39Intent intent = getIntent(); 40Class cls2 = R.drawable.class; 41try { 42//内部类使用外部局部变量,需要final 43int imgid2 = cls2.getField("img0"+finalI).getInt(null); 44intent.putExtra("image", imgid2); 45setResult(RESULT_OK, intent); 46finish(); 47} catch (Exception e) { 48e.printStackTrace(); 49} 50} 51}); 52 53} catch (Exception e) { 54e.printStackTrace(); 55} 56} 57 58 //for (int i = 0; i < iv.length; i++) { 59 //final int finalI = i; 60 ////给每一个ImageView找到id 61 //iv[i] = (ImageView) findViewById(ids[i]); 62 ////设置点击事件监听 63 //iv[i].setOnClickListener(new OnClickListener() { 64 //@Override 65 //public void onClick(View v) { 66 ////返回数据 67 //Intent intent = getIntent(); 68 ////内部类使用外部局部变量,需要final 69 //intent.putExtra("image", imgId[finalI]); 70 //setResult(RESULT_OK, intent); 71 //finish(); 72 //} 73 //}); 74 //} 75} 76 77 }
最终效果图
文章图片
推荐阅读
- android-Fragment
- Android自定义控件6--轮播图广告的实现
- Android自带图片裁剪
- css3扩展特性 – CSS3高级教程
- css3媒体查询和响应式设计 – CSS3高级教程
- 操作系统中的分段是怎么样的()
- PHP如何使用date_modify()函数(代码示例)
- Scala如何使用ListBuffer(详细代码解释)
- HTML如何使用id属性((唯一标识符))