Android 个人手机通讯录开发

一、Android 个人手机通讯录开发

  数据存储:SQLite 数据库

  开发工具:Android Studio

二、Phone Module 简介

1. 界面展示

              

2. 文件结构简单分析

三、个人手机通讯录代码实现

1. 清单文件 (AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.alan.directory" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon_phone"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 2. MainActivity.java (主文件)

/**
 * Created by Alan J on 13/2/2019.
 */

package com.example.alan.directory;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    MyHelper myHelper;
    private EditText etName;
    private EditText etPhone;
    private TextView tvShow;
    private Button btnAdd;
    private Button btnQuery;
    private Button btnUpdate;
    private Button btnDelete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myHelper = new MyHelper(this);
        init(); //初始化控件
    }
    private void init(){
        etName = (EditText)findViewById(R.id.et_name);
        etPhone = (EditText)findViewById(R.id.et_phone);
        tvShow = (TextView)findViewById(R.id.tv_show);
        btnAdd = (Button)findViewById(R.id.btn_add);
        btnQuery = (Button)findViewById(R.id.btn_query);
        btnUpdate = (Button)findViewById(R.id.btn_update);
        btnDelete = (Button)findViewById(R.id.btn_delete);
        btnAdd.setOnClickListener(this);          //Button控件设置监听
        btnQuery.setOnClickListener(this);
        btnUpdate.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        tvShow.setMovementMethod(ScrollingMovementMethod.getInstance());  //设置文本滚动
    }
    @Override
    public void onClick(View v){
        String name;
        String phone;
        SQLiteDatabase db;
        switch (v.getId()){
            case R.id.btn_add:       //添加联系人
                name = etName.getText().toString().trim();
                phone = etPhone.getText().toString().trim();
                db = myHelper.getWritableDatabase();
                if (name.equals("") || phone.equals("")){    //联系人信息不能为空
                    Toast.makeText(this,"联系人信息添加失败",Toast.LENGTH_SHORT).show();
                }
                else {
                    db.execSQL("insert into person (name,phone) values(?,?)", new Object[]{name, phone});
                    Toast.makeText(this,"联系人信息添加成功",Toast.LENGTH_SHORT).show();
                }
                db.close();
                break;
            case R.id.btn_query:    //查询联系人
                db = myHelper.getReadableDatabase();
                Cursor cursor = db.rawQuery("select name,phone from person",null);
                if (cursor.getCount() == 0){
                    tvShow.setText("");
                    Toast.makeText(this,"空目录",Toast.LENGTH_SHORT).show();
                }else {
                    cursor.moveToFirst();
                    tvShow.setText("Name:" + cursor.getString(0) + " ; Tel:" + cursor.getString(1));
                    while (cursor.moveToNext()){
                        tvShow.append("\n" + "Name:" + cursor.getString(0) + " ; Tel:" + cursor.getString(1));
                    }
                }
                cursor.close();
                db.close();
                break;
            case R.id.btn_update:    //修改联系人
                db = myHelper.getWritableDatabase();
                name = etName.getText().toString().trim();
                phone = etPhone.getText().toString().trim();
                if (name.equals("") || phone.equals("")){    //联系人信息不能为空
                    Toast.makeText(this,"联系人信息修改失败",Toast.LENGTH_SHORT).show();
                }
                else {
                    db.execSQL("update person set name=?,phone=? where name=?", new Object[]{name, phone, name});
                    Toast.makeText(this,"联系人信息修改成功",Toast.LENGTH_SHORT).show();
                }
                db.close();
                break;
            case R.id.btn_delete:   //删除联系人
                db = myHelper.getWritableDatabase();
                name = etName.getText().toString().trim();
                phone = etPhone.getText().toString().trim();
                if (name.equals("") || phone.equals("")){    //联系人信息不能为空
                    Toast.makeText(this,"联系人信息删除失败",Toast.LENGTH_SHORT).show();
                }
                else {
                    db.execSQL("delete from person where name=? and phone=?", new Object[]{name, phone});
                    Toast.makeText(this,"联系人信息删除成功",Toast.LENGTH_SHORT).show();
                }
                db.close();
                break;
        }
    }
}

3. MyHelper.java (数据库文件)

/**
 * Created by Alan J on 13/2/2019.
 */

package com.example.alan.directory;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyHelper extends SQLiteOpenHelper{

    public MyHelper(Context context){
        super(context, "alan.db", null ,2);
    }
    @Override

    public void onCreate(SQLiteDatabase db){
        db.execSQL("create table person(id integer primary key autoincrement,name varchar(20),phone varchar(20) unique)");
    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){

    }
}

4. activity_main.xml (XML Layout 布局文件)

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    android:background="@drawable/background"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lineOne">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/head"
            android:layout_margin="30dp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="通 讯 录"
            android:textSize="30dp"
            android:textStyle="bold"
            android:textColor="#BC8F8F"
            android:layout_gravity="center"
            android:layout_marginLeft="50dp"
            />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/lineTwo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lineOne"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="18dp"
        android:layout_marginRight="18dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓 名 : "
            android:textSize="18dp"
            android:textStyle="bold"/>
        <EditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="     请输入姓名"
            android:textSize="16dp"
            android:maxLength="14"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/lineTree"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lineTwo"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="18dp"
        android:layout_marginRight="18dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电 话 : "
            android:textSize="18dp"
            android:textStyle="bold"/>
        <EditText
            android:id="@+id/et_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="     请输入手机号码"
            android:textSize="16dp"
            android:maxLength="11"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lineFour"
        android:layout_below="@+id/lineTree"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="18dp"
        android:layout_marginRight="18dp"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/shape"
            android:layout_weight="1"
            android:text=" 添 加 "
            android:textSize="16dp"
            android:textColor="#c2c8ec"
            android:textStyle="bold"/>
        <Button
            android:id="@+id/btn_query"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/shape"
            android:layout_weight="1"
            android:layout_marginLeft="4dp"
            android:text=" 查 询 "
            android:textSize="16dp"
            android:textColor="#c2c8ec"
            android:textStyle="bold"/>
        <Button
            android:id="@+id/btn_update"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/shape"
            android:layout_weight="1"
            android:layout_marginLeft="4dp"
            android:text=" 修 改 "
            android:textSize="16dp"
            android:textColor="#c2c8ec"
            android:textStyle="bold"/>
        <Button
            android:id="@+id/btn_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/shape"
            android:layout_weight="1"
            android:layout_marginLeft="4dp"
            android:text=" 删 除 "
            android:textSize="16dp"
            android:textColor="#c2c8ec"
            android:textStyle="bold"/>
    </LinearLayout>
    <TextView
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:scrollbars="vertical"
        android:layout_below="@+id/lineFour"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="18dp"
        android:textSize="20dp"/>
</RelativeLayout>

5. shape.xml (Button 按钮设置)

<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <!--设置背景色-->
    <solid android:color="#BC8F8F" />

    <!--设置圆角-->
    <corners android:radius="105dip" />

    <!--设置边框线的宽度和颜色-->
    <stroke android:width="0dp" android:color="#B0C4DE" />
</shape>

四、Android 个人通讯录功能测试

1. 添加

分别添加联系人:姓名:小 明    电话:13888899922

         姓名:小 莉    电话:15866655588

添加联系人功能验证:姓名:小 明    电话:13888899922

             

添加联系人功能验证:姓名:小 莉    电话:15866655588

            

测试中的一些问题:1. 联系人电话号码不能重复添加,程序会终止退出,因为联系人的电话号码是唯一的(一个人可以有多个手机号,而一个手机号只能一个人使用 {该功能程序已经实现} )。

          2. 电话号码长度限制为11位。

          3. 联系人信息为空不能成功添加。

再次添加联系人:姓名:小 莉    电话:15866655588

                        

上述功能问题限制的重点代码如下:

//联系人电话号码唯一性

@Override

public void onCreate(SQLiteDatabase db){
        db.execSQL("create table person(id integer primary key autoincrement,name varchar(20),phone varchar(20) unique)");
}

//电话号码长度限制

<EditText
   android:id="@+id/et_phone"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:hint="     请输入手机号码"
   android:textSize="16dp"
   android:maxLength="11"/>

//联系人信息为空时的限制

        case R.id.btn_add:       //添加联系人
                name = etName.getText().toString().trim();
                phone = etPhone.getText().toString().trim();
                db = myHelper.getWritableDatabase();
                if (name.equals("") || phone.equals("")){    //联系人信息不能为空
                    Toast.makeText(this,"联系人信息添加失败",Toast.LENGTH_SHORT).show();
                }
                else {
                    db.execSQL("insert into person (name,phone) values(?,?)", new Object[]{name, phone});
                    Toast.makeText(this,"联系人信息添加成功",Toast.LENGTH_SHORT).show();
                }
                db.close();
                break;

2. 查询

查询通讯录联系人功能验证:

            

联系人查询重点代码:

//查询联系人

      case R.id.btn_query:
                db = myHelper.getReadableDatabase();
                Cursor cursor = db.rawQuery("select name,phone from person",null);
                if (cursor.getCount() == 0){
                    tvShow.setText("");
                    Toast.makeText(this,"空目录",Toast.LENGTH_SHORT).show();
                }else {
                    cursor.moveToFirst();
                    tvShow.setText("Name:" + cursor.getString(0) + " ; Tel:" + cursor.getString(1));
                    while (cursor.moveToNext()){
                        tvShow.append("\n" + "Name:" + cursor.getString(0) + " ; Tel:" + cursor.getString(1));
                    }
                }
                cursor.close();
                db.close();
                break;

3. 修改

修改联系人功能验证:姓名:小 明    电话:13888899922   ===》》》  姓名:小 明    电话:15888899922

注意小问题:必须输入联系人姓名和电话号码,才可以成功进行修改,在数据库中修改一句name字段值进行匹配

            

联系人修改重点代码:

//修改联系人

      case R.id.btn_update:
                db = myHelper.getWritableDatabase();
                name = etName.getText().toString().trim();
                phone = etPhone.getText().toString().trim();
                if (name.equals("") || phone.equals("")){    //联系人信息不能为空
                    Toast.makeText(this,"联系人信息修改失败",Toast.LENGTH_SHORT).show();
                }
                else {
                    db.execSQL("update person set name=?,phone=? where name=?", new Object[]{name, phone, name});
                    Toast.makeText(this,"联系人信息修改成功",Toast.LENGTH_SHORT).show();
                }
                db.close();
                break;

测试中的一些问题:联系人为空时不能进行修改

上述功能问题限制的重点代码如下:

         if (name.equals("") || phone.equals("")){    //联系人信息不能为空
                    Toast.makeText(this,"联系人信息修改失败",Toast.LENGTH_SHORT).show();
                }
                else {
                    db.execSQL("update person set name=?,phone=? where name=?", new Object[]{name, phone, name});
                    Toast.makeText(this,"联系人信息修改成功",Toast.LENGTH_SHORT).show();
                }

4. 删除

删除联系人功能验证:姓名:小 明    电话:15888899922

                        

联系人删除重点代码:

//删除联系人

       case R.id.btn_delete:
                db = myHelper.getWritableDatabase();
                name = etName.getText().toString().trim();
                phone = etPhone.getText().toString().trim();
                if (name.equals("") || phone.equals("")){    //联系人信息不能为空
                    Toast.makeText(this,"联系人信息删除失败",Toast.LENGTH_SHORT).show();
                }
                else {
                    db.execSQL("delete from person where name=? and phone=?", new Object[]{name, phone});
                    Toast.makeText(this,"联系人信息删除成功",Toast.LENGTH_SHORT).show();
                }
                db.close();
                break;

测试中的一些问题:联系人为空时不能进行删除

上述功能问题限制的重点代码如下:

         if (name.equals("") || phone.equals("")){    //联系人信息不能为空
                    Toast.makeText(this,"联系人信息删除失败",Toast.LENGTH_SHORT).show();
                }
                else {
                    db.execSQL("delete from person where name=? and phone=?", new Object[]{name, phone});
                    Toast.makeText(this,"联系人信息删除成功",Toast.LENGTH_SHORT).show();
                }

原文地址:https://www.cnblogs.com/prettygirll/p/10371967.html

时间: 2024-08-07 19:01:03

Android 个人手机通讯录开发的相关文章

Android安卓手机游戏开发

在android中,事件主要包括点击.长按.拖曳.滑动等操作,这些构成了Android的事件响应,总体来说,所有的事件都由如下三个部分作为基础构成: 按下(action_down),移动(action_move),抬起(action_up).各种响应归根结底都是基于View以及ViewGroup的,这两者中响应的方法分别有: View.java中: publi boolean dispatchTouchEvent(MotionEvent event) public boolean onTouch

Android 简易手机通讯录(源码)

简易手机通讯录 一:功能模块 1.主界面,通过listview 展示所有联系人信息,并在没有联系人时给出友好提示 2.主界面.显示最近查看的某个联系人信息 3.主界面,可以长按某联系人项,弹出菜单,删除该联系人;删除联系人后,在手机通知栏弹出信息给用户提示 4.主界面选择分组,可以查看所有联系人,也可以只查看某一分组的联系人 5.点击主界面的某联系人,可以进入联系人详情界面,查看联系人各详细信息,并可修改联系人各信息: 联系人的性别和所在分组可通过下拉菜单选择:长按电话号码,可以进入拨打电话功能

android 读取手机通讯录并显示listview

脉脉中注册时有一个,我已开启权限: 这个原理: 1.如果通讯录为空,脉脉就不让你往下进行, 2.如果没有开启权限,脉脉就不让你往下进行, 3.如果开启权限且通讯录为空,脉脉就不让你往下进行, 4.如果开启权限且通讯录不为空,脉脉才能让你往下进行. 读取通讯录权限 <!-- 读取联系人权限 --> <uses-permission android:name="android.permission.READ_CONTACTS" /> <!-- 拨打电话权限 -

Android自学历程—通讯录开发

前段时间写了个通讯录,现在闲的正好回顾回顾. 其实对我的感触是 一:实现方式有很多,那就要重于积累.但难免有落后的,设计不合理的. 二:还有就是设计方面,整体的设计上.编程不就是逻辑嘛.参考别人的,形成自己的. 1.显示效果,很简单,直接Listview 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.an

android获取手机通讯录

在android中读取联系人信息的程序,包括读取联系人姓名.手机号码和邮箱 (转载自博客:http://www.cnblogs.com/error404/archive/2013/03/12/2956090.html) 1:androidmanifest.xml的内容 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android

Android手机通讯录项目开发--联系人数据库contacts2.db介绍

项目描述:该项目为基于Android平台的手机通讯录,主要模块分为四个部分:联系人管理模块,通话记录管理模块,短信管理模块,系统设置模块. 系统结构图如下: 本项目启动时间:2014年5月28日 说明:本次开发项目的所有源码全部会分享给大家.开发此项目的目的有这几点:一.锻炼独立开发项目的能力,二.增加对Android开发的了解,三.熟悉Android通讯录机制. 闲话不多说,正式开始! 技术要点一:熟悉Android联系人数据库contacts2.db 1.获得联系人数据库contacts2.

Android开发之获取手机通讯录

获取手机通讯录是Android最常用的小功能,今天自学到了,记下来,主要是通过系统自带的内容提供者提供的数据,我们使用内容接收者获取相应的数据到cursor中,然后获取对应data表中的字段,相关字段代表什么含义,只能自己去查了. 下面是手机通讯录列表的代码,仅供参考: package com.andy.phonecontact; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import

【Android Demo】简单手机通讯录

Android 系统给我们提供了访问通讯录的接口,通过接口获取通讯录信息.Adapter 与 View 的连接主要依靠 getView 这个方法返回我们需要的自定义 view. ListView 是 Android App 中一个最最最常用的控件了,所以如何让 ListView 流畅运行,获取良好的用户体验是非常重要的. 对 ListView 优化就是对 Adapter 中的 getView 方法进行优化. 核心内容: 1.获取手机通讯录 2.数据封装 3.创建 Adapter 4.优化适配器

Android程序设计-简单手机通讯录

在微信中,手机QQ中你会发现软件读取手机通讯录这个功能,这个功能使得软件更好的与手机联系人绑定,从而达到分享,拨打电话,读取用户信息等操作.下面我们将通过一个demo实现这个功能 首先我们看一下效果图: -----------------------------------------------------------[正题部分]----------------------------------------------- [开发环境]Android Stdio 1.1.0 [分析] 软件中含