打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android 通讯录开发不完全总结2.添加联系人

这里主要总结的就是添加联系的代码编码编写

一.首先显然是得到空间的额引用,然后我们开始设置头像选择,首先找一组图片复制放到res下的drawable里。然后在代码里将这些图片放到一个数组咯,方便引用,只需要数组下标即可找到图片。private int[] images = {R.drawable.icon,

            R.drawable.image1, R.drawable.image2,

            R.drawable.image3, R.drawable.image4,

            R.drawable.image5, R.drawable.image6,

            R.drawable.image7, R.drawable.image8,

            R.drawable.image9, R.drawable.image10,

            R.drawable.image11, R.drawable.image12,

            R.drawable.image13, R.drawable.image14,

            R.drawable.image15, R.drawable.image16};

二.编写选择头像的对话框。

1.ImageButton设置监听器。

  btnImg.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                iniImageChoose();

            }

        });

2.iniImageChoose()方法。显示选择图片的对话框。

这个图片对话框,我们采用XML布局,然后将其扩展到我们的Dialog

先来看XML代码,这里主要有两个一个是Gallery还有另外一个是ImageSwitcher

前者主要是设置图片浏览控件的显示方式,而后者则是图片切换器的显示方式。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"

                android:layout_width = "fill_parent"

                android:layout_height = "fill_parent"

        >

    <Gallery

            android:id = "@+id/idgallery"

//这个是gallery的高度110px

            android:layout_height = "110px"

            android:layout_width = "fill_parent"

//和父控件的上面相差10px

            android:layout_marginTop = "10px"

//这个是和父控件的左对齐

            android:layout_alignParentLeft = "true"

            ></Gallery >

    <ImageSwitcher

            android:id = "@+id/idimgswtch"

//图片选择后显示的是90px的高度,90px的宽度

            android:layout_width = "90px"

            android:layout_height = "90px"

//顶部与父控件对齐

            android:layout_alignParentTop = "true"

//水平居中

            android:layout_centerHorizontal = "true"

//底部与gallery对齐

            android:layout_alignBottom = "@id/idgallery"

            ></ImageSwitcher >

</RelativeLayout >

3.iniImageChoose()方法里代码的编写。首先看代码 ,将在代码里注释

 public void iniImageChoose() {

/**AlertDialog有一个内部类Builder,我们用它来建立对话框

这个是它的两个构造函数:

AlertDialog.Builder(Context context)Constructor using a context for this builder and the AlertDialog it creates.

AlertDialog.Builder(Context context, int theme)Constructor using a context and theme for this builder and the AlertDialog it creates.

第二个时带对话框主题,这里我们自定义的,所以选择第一种。

**/

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

//设置标题:

        builder.setTitle("请选择头像");

//设置按钮。这个确定按钮是将我们选择的图片 返回到我们的添加联系人的Activity界面里

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialogInterface, int i) {

//将那个ImageButton 设置图片资源,调用父类ImageViewsetImageResourse方法,参数

                btnImg.setImageResource(images[imagePosition % images.length]);

            }

        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialogInterface, int i) {

            }

        });

//如果我们要把自己设置的LayoutXML文件显示出来,需要用到这个LayoutInflater布局扩展器,这个类被用来实例化布局到其相应XML文件视图中。这里我们用了它的静态方法from()从当前的上下文中获得扩展器。

        LayoutInflater inflater = LayoutInflater.from(this);

//这个我们使用LayoutInflater的方法扩张一个XML文件,由于返回一个VIew对象,所以我们用一个View对象接收

 //inflate(int resource, ViewGroup root)

//Inflate a new view hierarchy from the specified xml //resource.

        View v = inflater.inflate(R.layout.imageswitch, null);

//用这个试图获得其定义的对象那个的引用,这里先获得了gallery的引用 

       gallery = (Gallery) v.findViewById(R.id.idgallery);

//设置适配器,由于一个视图只是定义了数据的显示方法,并没与数据,所以需要一个连接数据与视图显示方式的桥梁,这个桥梁就是adapter

        gallery.setAdapter(new ImageAdapter(this));

//这个setSelection是设置默认的选择图片,我们设置图片资源中央的那张图片

        gallery.setSelection(images.length / 2);

//这个isImagerSwitch的实例对象 我们已将其定义为成员变量。并且让其获得对像的引//

        is = (ImageSwitcher) v.findViewById(R.id.idimgswtch);

//这里需要为这个ImageSwitch设置出厂,这个方法继承与ViewSwitcher。实现这个实现了ViewSwitcher.ViewFactory这个接口,这个接口里有个方法makeView。下面将会看到。   

     is.setFactory(new MyViewFactory(this));

//下面我们将这个视图设置到对话框里去

        builder.setView(v);

//让其显示

        builder.create().show();

//这个事gallery设置图片被选择监听器

        gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

//我们将被选择的图片的位置,传出去,方便在ImageButton的监听器调用显示。

                imagePosition = i;

//这里使用is的方法,将图片如何显示的方法

                is.setImageResource(images[i % images.length]);

            }

            public void onNothingSelected(AdapterView<?> adapterView) {

            }

        });

    }

。后续

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
2.5.9 AlertDialog(对话框)详解 | 菜鸟教程
《Android 对话框大全》 方法超简单 - Android新手入门 eoe·Andro...
Android中的普通对话框、单选对话框、多选对话框、带Icon的对话框、以及自定义Adapter和自定义View对话框详解
自定义Android 对话框(AlertDialog) 的样式
【Android】对话框 AlertDialog
对话框多种方式详解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服