场景
Android中点击按钮启动另一个Activity以及Activity之间传值:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103919470
在上面启动Activity和传值之后,怎样获取Acitvity的返回值。下面示例实现点击选择头像按钮,跳转到头像显示Activity,并将选择的图片的索引返回,在MainActivity中获取后设置头像。
效果
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
首先是主页面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); } }); } }