简单通讯录的实现
先来看一下我们需要实现的功能:
在这里我们需要实现的布局有三个:
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);//实例化
}
}
笔记贵在坚持,前一段时间有点放纵自己了,后面会继续坚持的。