当前位置: 移动技术网 > IT编程>移动开发>Android > Android 个人手机通讯录开发

Android 个人手机通讯录开发

2019年02月14日  | 移动技术网IT编程  | 我要评论

俨然造句,邻水163网址,闪吧字库

一、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();
                }

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网