Androidstudio开发,实现通讯录长按删除和拨打电话( 六)

1. 涉及到的技术点

  1. 数据库SQLite的使用
  2. ListView长按事件监听
  3. AlertDialog对话框的使用

2. 开发环境

  1. 开发工具:AndroidStudio
  2. 开发语言:java
  3. jdk版本:8.0+以上

3.需求分析

在第四集中,已经实现了使用ListView把通讯录联系人展示出来了,那么这期来实现,在选择联系人的时候,可以实现一键拨打电话,也可以实现删除通讯录联系人,把这两个功能实现

4. 实现步骤

  1. 监听ListView长按点击事件
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                 //这里做对应的逻辑处理
                return true;
            }
        });
  1. 当响应到长按事件后,弹出AlertDialog对话框

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("操作");
                builder.setSingleChoiceItems(new String[]{"拨打电话", "删除"}, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        currentIndex = which;
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (currentIndex == 0) {
                            Intent intent = new Intent();
                            intent.setAction(Intent.ACTION_DIAL);
                            intent.setData(Uri.parse("tel:" + item.getMobile()));
                            startActivity(intent);

                        } else {
                            mStudentDb.delete(item.get_id());
                            Toast.makeText(getActivity(), "删除成功", Toast.LENGTH_SHORT).show();
                            //刷新数据
                            mListAdapter.setStudentList(mStudentDb.queryListStudent());
                        }


                        currentIndex = 0;
                    }
                });
                builder.show();

温馨提示:完整代码,在第四集中已经给出来了 Androidstudio开发,ListView实现通讯录联系人列表( 四) https://blog.csdn.net/jky_yihuangxing/article/details/140919737

  1. 使用Intent 拨打电话实现
 Intent intent = new Intent();
 intent.setAction(Intent.ACTION_DIAL);
 //"tel:"为固定格式,后面平接手机号
 intent.setData(Uri.parse("tel:" + item.getMobile()));
 startActivity(intent);

5.运行效果

在这里插入图片描述

6.其它资料学习

  1. AlertDialog(对话框)详解: https://www.runoob.com/w3cnote/android-tutorial-alertdialog.html
  2. 其他几种常用对话框基本使用:https://www.runoob.com/w3cnote/android-tutorial-dialog.html
  3. ListView简单实用: https://www.runoob.com/w3cnote/android-tutorial-listview.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩宇软件开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值