MainActivity.java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0 && resultCode == 0){
Bundle bundle = data.getExtras();
int ImageId = bundle.getInt("imageId");
ImageView image = findViewById(R.id.image);
image.setImageResource(ImageId);
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Portrait.class);
startActivityForResult(intent,0);//0是请求码
}
});
}
}
activity_main.xml:
<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"
android:gravity="center"
>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/image"
android:src="@drawable/daoqi"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="更换头像"
android:id="@+id/btn"/>
</LinearLayout>
PortraitActivity.java:
public class LocationAcivity extends AppCompatActivity {
private int[] image = {R.drawable.img01, R.drawable.img02, R.drawable.img03};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_portrait);
GridView gv = findViewById(R.id.gv);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 0;i <image.length;i++) {
Map<String,Object> map = new HashMap<String, Object>();
map.put("image",image[i]);
list.add(map);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(
this,
list,
R.layout.item,
new String[]{"image"},
new int[]{R.id.touxiang}
);
gv.setAdapter(simpleAdapter);
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putInt("imageId",image[i]);
intent.putExtras(bundle);
setResult(0,intent);//0是返回码
finish();
}
});
}
}
activity_portrait.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="3"
android:id="@+id/gv">
</GridView>
</LinearLayout>
item.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/touxiang">
</ImageView>
</RelativeLayout>
效果图: