功能需求
一、在第一个页面点击一个按钮跳到第二个页面android
二、在第二个页面有用GridView布局 用来设置图片ide
三、在第二个页面 选中图片 后 返回第一个页面布局
四、在第一个页面显示 第二个页面选中的图片this
知识点:自定义适配器 -- simpleAdapterxml
GridView -- 布局对象
activity -- 页面之间的 跳转图片
---------------------------------------utf-8
一、在res/layout 有3个.xml 布局界面activity_main.xm.
head_image.xml image.xmlget
//先不界面 要用的控件布局好it
activity.xml -- 代码 -- 第一页布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="${relativePackage}.${activityClass}" >
<ImageView
android:id="@+id/mian_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:src="@drawable/img01"
/>
<Button
android:id="@+id/bt_choose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/mian_image"
android:text="选择头像"
android:onClick="choose"
/>
</RelativeLayout>
--------------------
head_image.xml -- 代码 -- 自定义simpleAdapter -- 的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="3"
/>
</LinearLayout>
---------------------
image.xml -- 代码 -- 显示从下一页 带回来选中的 图片
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
-----------------------------------
二、在MianActivity 类 实现 跳转的 功能
代码
public class MainActivity extends Activity {
private ImageView main_image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_image = (ImageView) this.findViewById(R.id.mian_image);
}
public void choose(View view){
Intent intent = new Intent(this,Header.class);
int requestCode = 100;
//第一种 开始跳转 页面的的 方法 有返回的 功能
startActivityForResult(intent, requestCode );
//第二种 开始跳转 页面的 的 方法 没有 返回的 功能
//startActivity(intent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 100 & resultCode == 101){
Bundle bundle = data.getExtras();
int id_image = bundle.getInt("image_id");
main_image.setImageResource(id_image);
}
}
}
------------------------------
三、实现下一个页面 要传输图像 回来的 功能
代码
public class Header extends Activity {
//自定义 适配器
private SimpleAdapter adapter;
//声明网格布局 对象
private GridView gridview;
private int[] head_image = {R.drawable.img01,R.drawable.img02,R.drawable.img03,
R.drawable.img04,R.drawable.img05,R.drawable.img06,R.drawable.img07,
R.drawable.img08,R.drawable.img09};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.head_image);
gridview = (GridView) this.findViewById(R.id.gridview);
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
for(int i = 0;i<head_image.length;i++){
Map<String, Object> map = new HashMap<String, Object>();
map.put("header", head_image[i]);
data.add(map);
}
//第三个参数 是 自定义 layout 里面的 xml文件
//第五个 参数 是 自定义layout里面的 xml 布局 里的 id
adapter = new SimpleAdapter(this, data , R.layout.image,
new String[] {"header"}, new int[] {R.id.image});
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { //getIntent 获取意图(那个页面 跳转 就 获取 那个页面 的 链接 的意思) Intent intent = getIntent(); Bundle bundle = new Bundle(); //获取 选择 图像 的位置 int image_id = head_image[position]; bundle.putInt("image_id", image_id); intent.putExtras(bundle); //设置 结果码 int resultCode = 101; setResult(resultCode , intent); finish(); } }); }}