1. 涉及到的技术点
- 数据库SQLite的使用
- ListView长按事件监听
- AlertDialog对话框的使用
2. 开发环境
- 开发工具:AndroidStudio
- 开发语言:java
- jdk版本:8.0+以上
3.需求分析
在第四集中,已经实现了使用ListView把通讯录联系人展示出来了,那么这期来实现,在选择联系人的时候,可以实现一键拨打电话,也可以实现删除通讯录联系人,把这两个功能实现
4. 实现步骤
- 监听ListView长按点击事件
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
//这里做对应的逻辑处理
return true;
}
});
- 当响应到长按事件后,弹出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
- 使用Intent 拨打电话实现
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DIAL);
//"tel:"为固定格式,后面平接手机号
intent.setData(Uri.parse("tel:" + item.getMobile()));
startActivity(intent);
5.运行效果
6.其它资料学习
- AlertDialog(对话框)详解: https://www.runoob.com/w3cnote/android-tutorial-alertdialog.html
- 其他几种常用对话框基本使用:https://www.runoob.com/w3cnote/android-tutorial-dialog.html
- ListView简单实用: https://www.runoob.com/w3cnote/android-tutorial-listview.html