Android中调用另一个Activity并返回结果-以模拟选择头像功能为例
场景
android中点击按钮启动另一个activity以及activity之间传值:
https://blog.csdn.net/badao_liumang_qizhi/article/details/103919470
在上面启动activity和传值之后,怎样获取acitvity的返回值。下面示例实现点击选择头像按钮,跳转到头像显示activity,并将选择的图片的索引返回,在mainactivity中获取后设置头像。
效果
注:
博客:
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
首先是主页面mainactivity的布局,添加一个选择头像按钮和一个imageview用来显示头像。
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".mainactivity"> <imageview android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <button android:id="@+id/button" android:text="选择头像" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </linearlayout>
然后在mainactivity中的oncreate方法中,使用startactivityforresult能启动另一个activity并能获得返回结果。
要设置一个请求码,这里是200。
button button = (button)findviewbyid(r.id.button); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(mainactivity.this,headactivity.class); startactivityforresult(intent,200); } });
然后跳转到第二个activity用来选择头像。首先设置其布局文件,添加一个gridview用来显示要选择的头像照片。
activity_head.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".headactivity"> <gridview android:id="@+id/gridview" android:numcolumns="4" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </linearlayout>
然后来到其activity,使用适配器为其设置照片源。
首先声明一个图片数组
private int[] imageid = new int[]{ r.drawable.img1,r.drawable.img2 };
这里引用了drawable下的两张头像照片。
然后使用适配器为照片设置数据源
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 null; } @override public long getitemid(int position) { return 0; } @override public view getview(int position, view convertview, viewgroup parent) { imageview imageview; if(convertview ==null) { imageview = new imageview(headactivity.this); imageview.setadjustviewbounds(true); imageview.setmaxwidth(158); imageview.setmaxheight(150); imageview.setpadding(5, 5, 5, 5); }else { imageview = (imageview) convertview; } imageview.setimageresource(imageid[position]); return imageview; } }; gridview.setadapter(adapter);
然后在gridview的选项点击事件监听器中,获取选中照片的索引并通过bundle对象的putint和intent对象的putextras将数据进行返回。然后调用 setresult(200,intent);将结果进行返回,这里的请求结果码也设置为200。
完整headactivity.java
package com.badao.selectimage; import androidx.appcompat.app.appcompatactivity; import android.content.intent; import android.os.bundle; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.baseadapter; import android.widget.gridview; import android.widget.imageview; public class headactivity extends appcompatactivity { private int[] imageid = new int[]{ r.drawable.img1,r.drawable.img2 }; @override protected void oncreate(bundle savedinstancestate) { 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 null; } @override public long getitemid(int position) { return 0; } @override public view getview(int position, view convertview, viewgroup parent) { imageview imageview; if(convertview ==null) { imageview = new imageview(headactivity.this); imageview.setadjustviewbounds(true); imageview.setmaxwidth(158); imageview.setmaxheight(150); imageview.setpadding(5, 5, 5, 5); }else { imageview = (imageview) convertview; } imageview.setimageresource(imageid[position]); return imageview; } }; gridview.setadapter(adapter); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_head); 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(200,intent); finish(); } }); } }
然后回到mainactivity怎样接受返回的结果。
ctrl + o重写方法 onactivityresult,如果请求码和返回结果码都是200,首先通过
bundle bundle = data.getextras();
获取bundle对象。
然后通过
int imageid = bundle.getint("imageid");
获取返回的照片索引数据。
然后给imageview设置照片源。
@override protected void onactivityresult(int requestcode, int resultcode, @nullable intent data) { super.onactivityresult(requestcode, resultcode, data); if(requestcode ==200 && resultcode == 200) { bundle bundle = data.getextras(); int imageid = bundle.getint("imageid"); imageview imageview = (imageview) findviewbyid(r.id.image); imageview.setimageresource(imageid); } }
mainactivity完整示例代码
package com.badao.selectimage; import androidx.annotation.nullable; import androidx.appcompat.app.appcompatactivity; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.imageview; public class mainactivity extends appcompatactivity { @override protected void onactivityresult(int requestcode, int resultcode, @nullable intent data) { super.onactivityresult(requestcode, resultcode, data); if(requestcode ==200 && resultcode == 200) { bundle bundle = data.getextras(); int imageid = bundle.getint("imageid"); imageview imageview = (imageview) findviewbyid(r.id.image); imageview.setimageresource(imageid); } } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button button = (button)findviewbyid(r.id.button); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(mainactivity.this,headactivity.class); startactivityforresult(intent,200); } }); } }