Android Studio实现简单的通讯录
作者:诸葛琴魔q 发布时间:2023-06-13 06:49:56
标签:Android,Studio,通讯录
网上找的一个单页面通讯录,修改之后将添加联系人和修改/删除联系人分为两个独立页面
MainActivity
package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
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 TextView tvShow;
private Button btnAdd;
private Button btnQuery;
private Button btnUpdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myHelper = new MyHelper(this);
init();
}
private void init(){
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);
btnAdd.setOnClickListener(this); //Button控件设置监听
btnQuery.setOnClickListener(this);
btnUpdate.setOnClickListener(this);
findViewById(R.id.traceroute_rootview).setOnClickListener(this);
tvShow.setMovementMethod(ScrollingMovementMethod.getInstance()); //设置文本滚动
}
public void onClick(View v){
SQLiteDatabase db;
switch (v.getId()){
case R.id.traceroute_rootview:
InputMethodManager imm=(InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(),0);
break;
case R.id.btn_add: //添加联系人
Intent intent=new Intent(MainActivity.this,nextActivity.class);
startActivity(intent);
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: //修改联系人
Intent intent1=new Intent(MainActivity.this,xiugaiActivity.class);
startActivity(intent1);
break;
}
}
}
nextActivity
package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class nextActivity extends AppCompatActivity implements View.OnClickListener {
MyHelper myHelper;
private EditText etName;
private EditText etPhone;
private Button btnAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
myHelper = new MyHelper(this);
init();
}
private void init(){
etName = (EditText)findViewById(R.id.et_name);
etPhone = (EditText)findViewById(R.id.et_phone);
btnAdd = (Button)findViewById(R.id.btn_add);
btnAdd.setOnClickListener(this); //Button控件设置监听
findViewById(R.id.traceroute_rootview).setOnClickListener(this);
}
public void onClick(View v){
String name;
String phone;
SQLiteDatabase db;
switch (v.getId()) {
case R.id.traceroute_rootview:
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
break;
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();
Intent intent=new Intent(nextActivity.this,MainActivity.class);
startActivity(intent);
break;
}
}
}
xiugaiActivity
package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class xiugaiActivity extends AppCompatActivity implements View.OnClickListener{
MyHelper myHelper;
private EditText etName;
private EditText etPhone;
private TextView tvShow;
private Button btnQuery;
private Button btnUpdate;
private Button btnDelete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xiugai);
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);
btnQuery = (Button)findViewById(R.id.btn_query);
btnUpdate = (Button)findViewById(R.id.btn_update);
btnDelete = (Button)findViewById(R.id.btn_delete);
btnQuery.setOnClickListener(this);
btnUpdate.setOnClickListener(this);
btnDelete.setOnClickListener(this);
findViewById(R.id.traceroute_rootview).setOnClickListener(this);
tvShow.setMovementMethod(ScrollingMovementMethod.getInstance()); //设置文本滚动
}
public void onClick(View v){
String name;
String phone;
SQLiteDatabase db;
switch (v.getId()){
case R.id.traceroute_rootview:
InputMethodManager imm=(InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(),0);
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;
}
}
}
MyHelper
package com.example.test;
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){
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/traceroute_rootview"
android:background="@color/white"
android:clickable="true"
android:gravity="center_horizontal"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="通 讯 录"
android:textSize="30dp"
android:textStyle="italic"
android:gravity="center"
android:textColor="@color/black">
</TextView>
<Button
android:id="@+id/btn_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape"
android:text=" 添加联系人"
android:textSize="16dp"
android:textColor="#c2c8ec"
android:textStyle="bold"/>
<Button
android:id="@+id/btn_query"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape"
android:text="查看联系人"
android:textSize="16dp"
android:textColor="#c2c8ec"
android:textStyle="bold"/>
<Button
android:id="@+id/btn_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape"
android:text=" 修改联系人"
android:textSize="16dp"
android:textColor="#c2c8ec"
android:textStyle="bold"/>
<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:textColor="#c2c8ec"
android:textSize="24dp"/>
</LinearLayout>
next.xml
<?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:id="@+id/traceroute_rootview"
android:background="@color/white"
android:clickable="true"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".nextActivity">
<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:maxLines="1"
android:singleLine="true"
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:maxLines="1"
android:singleLine="true"
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"/>
</LinearLayout>
</RelativeLayout>
xiugai.xml
<?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:id="@+id/traceroute_rootview"
android:background="@color/white"
android:clickable="true"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".xiugaiActivity">
<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:maxLines="1"
android:singleLine="true"
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:maxLines="1"
android:singleLine="true"
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_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:textColor="#c2c8ec"
android:textSize="24dp"/>
</RelativeLayout>
Mainfest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Test">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".nextActivity"></activity>
<activity android:name=".xiugaiActivity"></activity>
</application>
</manifest>
初学android,程序还存在许多bug,大家多提修改建议。
来源:https://blog.csdn.net/a92316/article/details/116135359


猜你喜欢
- 一、作用及种类UML类图建模语言或标准建模语言类的属性、操作中的可见性使用+、#、-分别表示public、protected、private
- 一.理论准备流是个抽象的概念,是对输入输出设备的抽象,Java程序中,对于数据的输入/输出操作都是以“流”的方式进行,设备可以是文件、网络、
- 牛逼!IDEA不愧为神器,结合Groovy脚本,简直天下无敌,如今, 有许许多多的插件或者编辑器都支持根据数据表自动生成数据实体类了, 比如
- 要说在 Spring Boot 中注册过滤器有三种方式,你都能想到哪些呢?今天松哥就来和大家聊一聊 Spring Boot 中注册过滤器的三
- 会话会话:用户打开浏览器进行的一系列操作直至关闭浏览器的过程看作是一次会话HTTP协议是无状态的,不能实现跟踪对话。比如进入一个网站,每次操
- 本文实例讲述了Android开发实现webview中img标签加载本地图片的方法。分享给大家供大家参考,具体如下:在网上查了很多教程,感觉很
- package airthmatic;public class demo10 { public static void main(
- 本文实例讲述了Java使用反射调用方法。分享给大家供大家参考,具体如下:一 代码import java.util.*;import java
- 实现说明这里的核心在于如何在大并发的情况下保证数据库能扛得住压力,因为大并发的瓶颈在于数据库。如果用户的请求直接从前端传到数据库,显然,数据
- 本文接上文“java反射之方法反射的基本操作方法”,利用反射了解下java集合中泛型的本质1、初始化两个集合,一个使用泛型,一个不使用Arr
- 背景在我们android开发中,如果需要actiivty/fragment等有状态的控件保存当前状态,由系统进行数据保存的恢复的时候比如正常
- * 什么是 * Spring MVC中的 * (Interceptor)类似于Servlet中的过滤器(Filter),它主要用于拦截用户
- 本文给大家分享Android视频播放器屏幕左侧边随手指上下滑动亮度调节功能的原理实现,具体代码如下所示:import android.app
- 最近单位又有一个新Java项目。涉及到扫码登录。之前项目使用的是 ajax轮询的方式。感觉太low了。所以这次用webSocket的方式进行
- 几个重要的函数:#include <pthread.h>int pthread_mutex_init(pthread_mutex
- 本文详细讲述了JAR命令的用法,对于大家学习和总结jar命令的使用有一定的帮助作用。具体如下:JAR包是Java中所特有一种压缩文档,其实大
- 前言:目前我们的项目是微服务架构,基于dubbo框架,服务之间的调用是通过rpc调用的。刚开始没有任何问题,项目运行健康、良好。可是过了一段
- 背景传统 SpringMVC 项目中,我们可以定义容器初始化 Servlet ,然后在 web.xml 配置该 Servlet ,指定 lo
- PowerPoint幻灯片中可插入公式,用于在幻灯片放映时演示相关内容的论证、推算的依据,能有效地为演讲者提供论述的数据支撑。通过后端程序代
- 本文实例为大家分享了Unity3d实现跑马灯广播效果的具体代码,供大家参考,具体内容如下废话不多说,直接上代码using DG.Tweeni