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
0
投稿
猜你喜欢
- 前言在淘宝内网里看到同事发了贴说了一个CPU被100%的线上故障,并且这个事发生了很多次,原因是在Java语言在并 * 况下使用HashMap
- 一、自带函数助手MD5加密在函数助手中找到__MD5这个函数,第一个参数是要md5加密的值,第二个参数是保存加密后值的变量在请求参数中引用该
- Java Double相加出现的怪事问题的提出编译运行下面这个程序会看到什么public class test { public stati
- 一:问题描述 在已经root过的android设备下,app执行一个linux命令,app需要获取su权限,在某些a
- 上一篇:瑞吉外卖项目:新增员工一. 员工信息分页查询1. 需求分析当系统中的用户越来越多页面展示不完整,我们需要通过实现分页的方式去展示员工
- 本文实例讲述了Java正则验证IP的方法。分享给大家供大家参考,具体如下:网上用正则验证IP的表达式有很多,一搜一大堆,可以自己写,但很麻烦
- 一、万能的字符串当然,任何时候都可以使用字符串作为属性的值,从配置文件里读取出来,如下:配置文件内容为:pkslow.admin=larry
- 本文实例为大家分享了java导出csv格式文件的具体代码,供大家参考,具体内容如下导出csv格式文件的本质是导出以逗号为分隔的文本数据imp
- 目录写在前面引入guava依赖包怎么做变量转换写在前面有时候需要处理对象属性的getter、setter方法,或者将属性与数据表字段进行相互
- 带logo的二维码生成分为两步骤:首先根据输入的内容生成二维码图片,然后读取本地的logo图片,通过图片处理生成带logo的二维码。生成的二
- 我就废话不多说了,大家还是直接看代码吧~ public static void main(String[] args) { &n
- Zuul 简介Zuul 的主要功能是路由转发和过滤器。路由功能是微服务的一部分,比如 /api/admin 转发到到 Admin 服务,/a
- 为了解决在多个窗口之间的传值问题,我们可以通过设置静态类和静态变量的办法来实现窗口间值的传递窗体一代码//窗体1的代码using Syste
- 为什么要给图片添加水印为图片添加水印的主要作用是保护图片版权,防止图片被未经授权的人使用或传播。通常情况下,图片水印会包含图片作者的名字、版
- springboot中集成jpa需要再pom文件中添加jpa的jar包,使用springboot的话iju不用自己规定版本号了,自动管理依赖
- 本文项目为大家分享了C#实现点餐系统,供大家参考,具体内容如下项目介绍:一家店铺使用的外卖点餐系统本项目分三大模块:登录注册模块,用户模块,
- Redis中opsForValue()方法的使用1、set(K key, V value)新增一个字符串类型的值,key是键,value是值
- 前面几篇文章分别讨论了Activity和Service,本文就来讨论BroastcastReceiver,Broastcast是应用程序间通
- 1. RocketMQ Topic创建机制以下源码基于Rocket MQ 4.7.0RocketMQ Topic创建机制分为两种:一种自动创
- 今天在做数据分页显示的时候遇到了一个问题,经过测试,证实是Tomcat 6的一个bug,我所用的版本为:apache-tomcat-6.0.