图像选择:Main中点击选择图像按钮,启动Head活动用来选择图像,
然后将被选择图像通过bundle发送给Main,同时附带返回值,方便Main来判断
Head布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context=".HeadActivity"> <GridView android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" android:horizontalSpacing="3dp" android:verticalSpacing="3dp" android:numColumns="2" > </GridView> </RelativeLayout>
Main布局文件
<?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" tools:context=".MainActivity" android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/b" > </ImageView> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="更换图片" /> </LinearLayout>
Head java代码
package com.example.myactivityiii; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.media.Image; 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.a,R.drawable.b,R.drawable.c,R.drawable.d }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_head); //给图像视图创建适配器 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//返回一个ImageView public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if(convertView == null){ imageView = new ImageView(HeadActivity.this); } else { imageView = (ImageView)convertView; } imageView.setImageResource(imageId[position]); return imageView; } }; gridView.setAdapter(adapter); //选项被点击的监听器 gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override//获取选择头像的id,保存到数据包中,传回到Main中 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(); } }); } }
Main java代码
package com.example.myactivityiii; 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; /* * 使用startActivityForResult,调用一个Activity并返回结果 * */ public class MainActivity extends AppCompatActivity { //判断请求码与返回码是否与之前设置的相同,如果相同则获取传递的数据包 @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); //1.判断请求码与返回码是否与之前设置的相同 if(requestCode == 0x11 && resultCode == 0x11){ //2.获取在Head活动中传递的数据 Bundle bundle = data.getExtras(); int imageId = bundle.getInt("imageId"); //3.在Main中的ImageView中更新 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,0x11); } }); } }