欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Studio实现简单的通讯录

程序员文章站 2022-03-17 23:08:36
网上找的一个单页面通讯录,修改之后将添加联系人和修改/删除联系人分为两个独立页面mainactivitypackage com.example.test; import androidx.appcom...

网上找的一个单页面通讯录,修改之后将添加联系人和修改/删除联系人分为两个独立页面

Android Studio实现简单的通讯录Android Studio实现简单的通讯录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,大家多提修改建议。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。