通讯录

  1. 布局文件
  2. 界面交互代码

实现一个简单的通讯录app

布局文件

总体是一个相对布局,姓名栏和电话栏还有按钮分别是三个线性布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ll_name"
android:layout_marginTop="200sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:id="@+id/tv_usr"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_name"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ll_phone"
android:layout_below="@+id/ll_name">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电话:"
android:id="@+id/tv_phone"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_phone"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ll_phone"
android:layout_marginTop="20sp"
android:id="@+id/ll_btn"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_add"
android:layout_weight="1"
android:text="添加"/>
<Button
android:layout_weight="1"
android:id="@+id/btn_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"/>
<Button
android:layout_weight="1"
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改"/>
<Button
android:layout_weight="1"
android:id="@+id/btn_get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询"/>

</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ll_btn"
android:id="@+id/btn_call"
android:textSize="15sp"
android:text="拨打该号码"/>

</RelativeLayout>

界面交互代码

主要需要实现两个功能,即对联系人的增删改查和拨打号码

对联系人的操作需要用SQLite

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class MyHelper extends SQLiteOpenHelper{

public MyHelper(Context context) {
super(context, "information", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE information(_id integer primary key,name varchar(20),phone varchar(20))");
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}

拨打号码需要用到intent,设置一个隐式意图,指定ACTION_DIAL实现拨打号码的功能

定义一个MyHelper类继承自SQLIteOpenHelper,在onCreate中执行创建table的语句,定义了一个id作为主键,字符类型的姓名和电话号码

因为涉及的操作按钮比较多,所以主函数可以通过实现View.onClickListener接口的功能和switch语句更加方便的实现按钮的响应

整体代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package cn.n1ng.addressbook;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private MyHelper myHelper;
private EditText et_name;
private EditText et_phone;
private Button btn_add;
private Button btn_del;
private Button btn_update;
private Button btn_get;
private Button btn_call;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myHelper = new MyHelper(this);
init();
}

private void dialPhoneNumber(String phoneNumber){
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+phoneNumber));
startActivity(intent);
}
private void init() {
et_name = (EditText) findViewById(R.id.et_name);
et_phone = (EditText) findViewById(R.id.et_phone);
btn_add = (Button) findViewById(R.id.btn_add);
btn_del = (Button) findViewById(R.id.btn_del);
btn_update = (Button) findViewById(R.id.btn_update);
btn_get = (Button) findViewById(R.id.btn_get);
btn_call = (Button) findViewById(R.id.btn_call);
btn_add.setOnClickListener(this);
btn_del.setOnClickListener(this);
btn_update.setOnClickListener(this);
btn_get.setOnClickListener(this);
btn_call.setOnClickListener(this);
}

@SuppressLint("Range")
@Override
public void onClick(View view) {
String name ;
String phone;
SQLiteDatabase db;
ContentValues values;
switch (view.getId()){
case R.id.btn_add://JDK17以上需要在gradle.properties中添加”android.nonFinalResIds=false“否则会报错
db = myHelper.getWritableDatabase();
name = et_name.getText().toString();
phone = et_phone.getText().toString();
values = new ContentValues();
values.put("name",name);
values.put("phone",phone);
db.insert("information",null,values);
Toast.makeText(this,"添加成功", Toast.LENGTH_SHORT).show();
db.close();
et_phone.setText("");
et_name.setText("");
break;
case R.id.btn_del:
db = myHelper.getWritableDatabase();
name = et_name.getText().toString();
db.delete("information","name = ?",new String[]{name});
Toast.makeText(this,"删除成功", Toast.LENGTH_SHORT).show();
db.close();
et_phone.setText("");
et_name.setText("");
break;
case R.id.btn_update:
db = myHelper.getWritableDatabase();
name = et_name.getText().toString();
phone = et_phone.getText().toString();
values = new ContentValues();
values.put("phone",phone);
db.update("information",values,"where name = ?",new String[]{name});
Toast.makeText(this,"更新成功", Toast.LENGTH_SHORT).show();
db.close();
et_phone.setText("");
et_name.setText("");
break;
case R.id.btn_get:
db = myHelper.getWritableDatabase();
name = et_name.getText().toString();
Cursor cursor = db.query("information",null,null,null,null,null,null);
if(cursor.getCount()==0){
Toast.makeText(this,"没有数据", Toast.LENGTH_SHORT).show();
}
while (cursor.moveToNext()){
if(name.equals(cursor.getString(cursor.getColumnIndex("name")))){
et_phone.setText(cursor.getString(cursor.getColumnIndex("phone")));
Toast.makeText(this,"查找成功", Toast.LENGTH_SHORT).show();
}
}
break;
case R.id.btn_call:
phone = et_phone.getText().toString();
dialPhoneNumber(phone);
break;
}
}
class MyHelper extends SQLiteOpenHelper{

public MyHelper(Context context) {
super(context, "information", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE information(_id integer primary key,name varchar(20),phone varchar(20))");
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}
}
}



转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 3049155267@qq.com

💰

×

Help us with donation