Android学习笔记八------简单通讯录的实现

简单通讯录的实现
先来看一下我们需要实现的功能:
在这里插入图片描述
在这里我们需要实现的布局有三个:
1.初始界面的布局;以及存入后的相关显示
2.联系人信息显示框的布局;
3.添加界面的布局
在此基础上实现各个布局所对应的功能;
首先我们先来了解一下我们所要应用到的一个组件:ListView
ListView是android.widget.AbsListView的子类,主要用来以列表方式显示一些内容。开发时一般有以下两个功能:
1.将数据填充到布局;
2.处理用户的选择点击操作。
在使用ListView组件开发时必须包含3个关键要素:
1.ListView中每一行的View;每一行可以显示多个也可以显示单个
2.填入到View中的数据(被映射的字符串、图片或基本组件)
3.链接数据与ListView的Adapter(适配器)。
在布局中添加ListView组件:(ListView需要放在RelativeLayout后面)

<ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
    <!--可以自定义id-->

完整的main.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--
    在线性布局中添加相对布局,进行设计主界面
    运用ListView控件实现对返回数据的显示需要注意的是应该注意各个部件的位置及相应的大小
    -->
<RelativeLayout
    android:background="#000000"
    android:layout_width="match_parent"
    android:layout_height="35dp">

    <TextView
        android:textColor="#ffffff"
        android:text="通讯录"
        android:textSize="25sp"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/btnAdd"
        android:text="添加"
        android:textColor="#ffffff"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
</LinearLayout>

然后看一下我们的联系人信息显示框的布局;很简单,只要放置好相应的组件就可以了,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--
    重新设置一个布局来实现通讯录中每个人信息的显示
    -->
<ImageView
    android:src="@mipmap/man"
    android:id="@+id/img"
    android:layout_width="40dp"
    android:layout_height="40dp"/>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tvname"
            android:text="张三"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/tvtel"
            android:text="110"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

第三个布局:添加界面的布局:
也是在里面进行各个组件的放置;应用嵌套式布局进行设计;
在各个子布局中进行相应的设计;
代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--
    在添加页面的布局中运用LinearLayout,EditText和LinearLayout实现布局
    在各个LinearLayout中实现相应的ImageView,TextView或者Button布局
    -->
    <LinearLayout
        android:background="#000000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/imgreturn"
            android:src="@mipmap/back1"
            android:layout_width="40dp"
            android:layout_height="40dp"/>
        <TextView
            android:textSize="30sp"
            android:text="返回"
            android:textColor="#ffffff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <EditText
        android:id="@+id/addname"
        android:text="请输入姓名"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <EditText
        android:id="@+id/addtel"
        android:text="请输入电话号码"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <LinearLayout
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:src="@mipmap/back1"
            android:id="@+id/left"
            android:layout_width="80dp"
            android:layout_height="80dp"/>
        <ImageView
            android:src="@mipmap/man"
            android:id="@+id/select"
            android:layout_width="80dp"
            android:layout_height="80dp"/>
        <ImageView
            android:src="@mipmap/next"
            android:id="@+id/right"
            android:layout_width="80dp"
            android:layout_height="80dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btnreset"
            android:text="重置"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/btnadd"
            android:text="添加"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>


需要创建新的activity来实现添加功能;即多线程应用;
在mainactivity中实现点击按钮对addactivity的启动:
在给按钮定义单击事件时,应用Intent方法进行使用:

 btnAdd.setOnClickListener(new View.OnClickListener() {//重写监听事件,重写setOnClickListener()方法
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,AddActivity.class);
                startActivity(intent);
            }
        });

为Adapter装配数据:

//第一个参数:上下文;第三个参数:适配器展示时需要的是那个布局文件;第四个参数:一个数组;
            adapter = new SimpleAdapter(MainActivity.this, list, R.layout.item, from, to);

在addactivity中实现各个按钮的功能,进行监听;
并且实现添加功能,将数据保存起来,并在返回时可以将数据返回;
要实现数据的监听,需要定义一个Person类进行封装:
包含姓名,头像id,电话号码;
代码如下:

package com.example.lesson9_tongxunlu;

import java.io.Serializable;
//使用ListView方法是需要定义接口,以实现数据的有效回传
public class Person implements Serializable {//对person类进行序列化
    private String lxrname;
    private String lxrtel;
    private int lxrid;

    public Person(String lxrname,String lxrtel,int lxrid) {

        this.lxrname = lxrname;
        this.lxrtel = lxrtel;
        this.lxrid = lxrid;
    }
    //设置和获取对应的值,用系统生成的方法
    public String getLxrname() {
        return lxrname;
    }
    public void setLxrname(String lxrname) {
        this.lxrname = lxrname;
    }
    public String getLxrtel() {
        return lxrtel;
    }
    public void setLxrtel(String lxrtel) {
        this.lxrtel = lxrtel;
    }
    public int getLxrid() {
        return lxrid;
    }
    public void setLxrid(int lxrid) {
        this.lxrid = lxrid;
    }
}

在addactivity中定义Person类;定义添加页面中添加按钮的监听事件:

btnadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Person person = new Person(addname.getText().toString(),addtel.getText().toString(),imgID[index]);
                persons.add(person);//将person加入到persons当中
                flag = true;
            }
        });

完整的AddActivity代码如下:

package com.example.lesson9_tongxunlu;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import java.util.ArrayList;

public class AddActivity extends AppCompatActivity {
    private EditText addname,addtel;
    private ImageView imaleft,imgselect,imgright,imgreturn;
    private Button btnreset,btnadd;
    //用数组来存放可选头像
    private int imgID[]={R.mipmap.man,R.mipmap.women,R.mipmap.older,R.mipmap.child};
    //定义index
    private int index = 0;
    private ArrayList<Person> persons = new ArrayList<>();//定义为ArrayList<Person>,用幻形的方法生成类型
    private  boolean flag = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.add);
        initView();

        imaleft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//重写imlefa按钮的监听事件
                index--;
                if (index<0)index = imgID.length-1;//如果小于零则跳转到最后一个;实现图片的循环;
                imgselect.setImageResource(imgID[index]);
            }
        });
        imgright.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index++;
                if (index>imgID.length-1)index = 0;//循环选择图片
                imgselect.setImageResource(imgID[index]);
            }
        });

        btnadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Person person = new Person(addname.getText().toString(),addtel.getText().toString(),imgID[index]);
                persons.add(person);//将person加入到persons当中
                flag = true;
            }
        });

        imgreturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//重写返回图片卡的监听事件
                Intent intent = new Intent(AddActivity.this,MainActivity.class);
                //将数据传回去:
                intent.putExtra("persons",persons);//persons的内容
                intent.putExtra("flag",flag);//flag的内容
                startActivity(intent);
            }
        });

    }

    void initView(){//通过findViewById查找id以便后续的使用
        addname = (EditText) this.findViewById(R.id.addname);
        addtel = (EditText) this.findViewById(R.id.addtel);
        imaleft = (ImageView) this.findViewById(R.id.left);
        imgselect = (ImageView) this.findViewById(R.id.select);
        imgright = (ImageView) this.findViewById(R.id.right);
        imgreturn = (ImageView) this.findViewById(R.id.imgreturn);
        btnreset = (Button) this.findViewById(R.id.btnreset);
        btnadd = (Button) this.findViewById(R.id.btnadd);
    }
}

完整的MainActyivity代码如下:

package com.example.lesson9_tongxunlu;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
    Button btnAdd;
    TextView textView;
    SimpleAdapter adapter;//将传回的数据在主界面进行显示
    ArrayList<Person> persons;//定义传回来的数据
    ArrayList list = new ArrayList();//存放数据
    ListView listView;//定义listView
    boolean flag = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);
        initView();
        Intent intent = getIntent();//存传回来的数据;
        persons = (ArrayList<Person>) intent.getSerializableExtra("persons");//实例化传回来的值
        flag = intent.getBooleanExtra("flag",false);//取值flag
        if (flag) {//判断flag,如果为ture才进行操作,为false则不进行操作
            //放到HashMap,增加到对应的数组;
            for (int i = 0; i < persons.size(); i++) {
                HashMap map = new HashMap();
                map.put("imgid", persons.get(i).getLxrid());
                map.put("name", persons.get(i).getLxrname());
                map.put("tel", persons.get(i).getLxrtel());
                list.add(map);//数据的绑定
            }
            //下面的第四个参数即
            String[] from = {"imgid", "name", "tel"};
            int[] to = {R.id.img, R.id.tvname, R.id.tvtel};
            //第一个参数:上下文;第三个参数:适配器展示时需要的是那个布局文件;第四个参数:一个数组;
            adapter = new SimpleAdapter(MainActivity.this, list, R.layout.item, from, to);
            listView.setAdapter(adapter);//适配器进行适配
        }
        btnAdd.setOnClickListener(new View.OnClickListener() {//重写监听事件,重写setOnClickListener()方法
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,AddActivity.class);
                startActivity(intent);
            }
        });
    }

    void  initView(){
        btnAdd = (Button) this.findViewById(R.id.btnAdd);
        listView = this.findViewById(R.id.lv);//实例化
    }
}

笔记贵在坚持,前一段时间有点放纵自己了,后面会继续坚持的。

目 录 1 前言 1 2 系统的需求分析 2 2.1 需求分析 2 2.1.1 基本功能需求 2 2.1.2 系统用例分析 2 2.2 总体设计方案 5 2.2.1 系统模块关系与划分 5 3 系统的概要设计 6 3.1 通讯录需求分析 6 3.1.1 新建、编辑联系人 6 3.1.2 查找联系人 6 3.1.3 通讯功能 6 3.1.4 个人中心 7 3.1.5 还原与备份功能 7 3.2 系统界面设计 8 4 系统编码实现 14 前 言 随着移动通信与Internet向移动终端的普及,网络和用户对移动终端的要求越来越高 ,而Symbian,Windows Mobile,PalmOS等手机平台过于封闭,不能很好的满足用户的需求,因此市场迫切需要 一个开发性很强的平台。经过多年的发展,第三代数字通信(3G)技术活动了广泛的接 受,它为移动终端用户带来了更快的数据传输速率。随着3G网络的使用,移动终端不再 仅是通讯网络的终端,还将成为互联网的终端。因此,移动终端的应用软件和需要的服 务将会有很大的发展空间。Google为此与2007年11月推出了一个专为移动设备设计的软 件平台——AndroidAndroid 是一套真正意义上的开发性的移动设备综合平台,它包括操作系统、中间件和一些关键 的平台应用。Android 是由Linux+Java构成的开源软件,允许所有厂商和个人在其基础上进行开发Android平 台的开放性等特点既能促进技术(包括平台本身)的创新,又有助于降低开发成本,还 可以是运营商能非常方便地制定自己的特色化的产品。因此,它具有很大的市场发展潜 力。 Android(Google公司)是Google开发的基于Linux平台的开源手机操作系统。它包括 操作系统、用户界面和应用程序 ——移动电话工作所需的全部软件,而且不存在任何以往阻碍移动产业创新的专有权障碍 。谷歌与开放手机联盟合作开发Android,这个联盟由包括中国移动、摩托罗拉、高通、宏达和 T-Mobile 在内的 30 多家技术和无线应用的领军企业组成。 1) 优点:具备触摸屏、高级图形显示和上网功能,界面强大,可以说是一种 融入全部Web应用的单一平台 2) 缺点:由于时时刻刻都需要和网络进行连接,因此在手机的能耗方面控制就较差, 导致待机能力不足;又由于其开源性,过分依赖开发商,缺少标准配置。 1. 需求分析 1. 基本功能需求 能要求:实现通信录的在线备份还原功能,能把系统的通信录一键导入导出。 实现要求:客户端基于Android平台实现,服务端技术自定 用例场景:小明丢了手机,只好去抢购了一个小米同时把手机卡补办回来,需要把之 前手机的200个联系人补上。好在小明之前把所有联系人都备份到服务器了,只需要下载 在线通信录后,登录平台,一键还原即可。 2. 系统用例分析 图1- 1显示了通话记录功能模块。包括了联系人详细信息查看,清空通话记录,在选择一个条 目后,可以对其进行拨打电话,发送短信功能的操作,也可以进行删除。 手机用户 图1-1 通话记录模块用例图 图1- 2显示了联系人功能模块的用例。包括了查看联系人详细信息,编辑联系人信息,新建联 系人,对选中的联系人,可以对其进行拨打电话、发送短信的操作。用户还可以进行联 系人搜索,这样方便用户快速找到想找的联系人信息。 图1-2 电话薄模块用例图 图1- 3为个人中心模块中设置个人详细信息子模块的用例。该模块的功能就是用户设置自己的 个人基本信息。 图1-3 设置个人信息模块用例图 图1-4显示了联系人还原与备份的用例图。该模块的功能就是联系人的还原与备份 图1-4 联系人导入导出 2. 总体设计方案 1. 系统模块关系与划分 一个好的系统设计的步骤决定了程序是否能按照设计者的目的按时完成,是否能在规定 的时间内按照设计者的要求高质量的完成程序必要的功能。并且按照标准的设计步骤对 程序进行调试,测试,以及后期的优化完善,使程序更加具有健壮性和可用性。通过对 通讯录功能、系统模块、用户需求方面进行全方位的分析制定开发流程。 采用标准的开发流程确定系统具有用户管理功能,联系人增删改功能,通讯功能,查找 功能,备份等功能。 图1-5 系统功能图 通过对系统的功能结构的分析,设计后系统运行流程是系统运行后用户将进入通讯录 主界面,可以看到联系人,增加联系人,。通过点击MENU界面的增加功能选项可以新增 联系人,通过查找按钮可以对联系人进行姓名、号码的操作。通过个人中心可以显示所 有联系人、还原所有联系人、并备份功能。在联系人详细信息界面点击MENU键弹出通讯 功能框选择拨打电话按钮或者发信息发邮件按键,系统的运行流程图如1-6所示。 图1-6 系统运行流程 2.1通讯录需求分析 根据手机功能调查显
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

入门就入土&小迷弟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值