a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

Android個人手機通訊錄開發詳解

2019-10-21 21:25:25
字體:
來源:轉載
供稿:網友

一、Android 個人手機通訊錄開發

數據存儲:SQLite 數據庫

開發工具:Android Studio

二、Phone Module 簡介

1. 界面展示

Android,手機通訊錄

Android,手機通訊錄

2. 文件結構簡單分析

Android,手機通訊錄

三、個人手機通訊錄代碼實現

1. 清單文件 (AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.alan.directory" > <application  android:allowBackup="true"  android:icon="@drawable/icon_phone"  android:label="@string/app_name"  android:supportsRtl="true"  android:theme="@style/AppTheme" >  <activity android:name=".MainActivity" >   <intent-filter>    <action android:name="android.intent.action.MAIN" />    <category android:name="android.intent.category.LAUNCHER" />   </intent-filter>  </activity> </application></manifest>

 

2. MainActivity.java (主文件)

/** * Created by Alan J on 13/2/2019. */package com.example.alan.directory;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.method.ScrollingMovementMethod;import android.view.View;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 EditText etName; private EditText etPhone; private TextView tvShow; private Button btnAdd; private Button btnQuery; private Button btnUpdate; private Button btnDelete; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  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);  btnAdd = (Button)findViewById(R.id.btn_add);  btnQuery = (Button)findViewById(R.id.btn_query);  btnUpdate = (Button)findViewById(R.id.btn_update);  btnDelete = (Button)findViewById(R.id.btn_delete);  btnAdd.setOnClickListener(this);   //Button控件設置監聽  btnQuery.setOnClickListener(this);  btnUpdate.setOnClickListener(this);  btnDelete.setOnClickListener(this);  tvShow.setMovementMethod(ScrollingMovementMethod.getInstance()); //設置文本滾動 } @Override public void onClick(View v){  String name;  String phone;  SQLiteDatabase db;  switch (v.getId()){   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();    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;  } }}

 

3. MyHelper.java (數據庫文件)

/** * Created by Alan J on 13/2/2019. */package com.example.alan.directory;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){ }}

 

4. activity_main.xml (XML Layout 布局文件)

<?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:background="@drawable/background" tools:context=".MainActivity"> <LinearLayout  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:id="@+id/lineOne">  <ImageView   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:background="@drawable/head"   android:layout_margin="30dp"/>  <TextView   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="通 訊 錄"   android:textSize="30dp"   android:textStyle="bold"   android:textColor="#BC8F8F"   android:layout_gravity="center"   android:layout_marginLeft="50dp"   /> </LinearLayout> <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: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: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"/>  <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:textSize="20dp"/></RelativeLayout>

 

5. shape.xml (Button 按鈕設置)

<?xml version="1.0" encoding="utf-8" ?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!--設置背景色--> <solid android:color="#BC8F8F" /> <!--設置圓角--> <corners android:radius="105dip" /> <!--設置邊框線的寬度和顏色--> <stroke android:width="0dp" android:color="#B0C4DE" /></shape>

 

四、Android 個人通訊錄功能測試

1. 添加

分別添加聯系人:姓名:小 明 電話:13888899922

         姓名:小 莉 電話:15866655588

添加聯系人功能驗證:姓名:小 明 電話:13888899922

Android,手機通訊錄

Android,手機通訊錄

添加聯系人功能驗證:姓名:小 莉 電話:15866655588

Android,手機通訊錄

Android,手機通訊錄

測試中的一些問題:1. 聯系人電話號碼不能重復添加,程序會終止退出,因為聯系人的電話號碼是唯一的(一個人可以有多個手機號,而一個手機號只能一個人使用 {該功能程序已經實現} )。

          2. 電話號碼長度限制為11位。

          3. 聯系人信息為空不能成功添加。

再次添加聯系人:姓名:小 莉 電話:15866655588

Android,手機通訊錄

Android,手機通訊錄

Android,手機通訊錄

 

上述功能問題限制的重點代碼如下:

//聯系人電話號碼唯一性@Overridepublic void onCreate(SQLiteDatabase db){  db.execSQL("create table person(id integer primary key autoincrement,name varchar(20),phone varchar(20) unique)");}//電話號碼長度限制<EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="  請輸入手機號碼" android:textSize="16dp" android:maxLength="11"/>//聯系人信息為空時的限制        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();    break;

 

2. 查詢

查詢通訊錄聯系人功能驗證:

Android,手機通訊錄

Android,手機通訊錄

 

聯系人查詢重點代碼:

//查詢聯系人      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;

 

3. 修改

修改聯系人功能驗證:姓名:小 明 電話:13888899922 ===》》》 姓名:小 明 電話:15888899922

注意小問題:必須輸入聯系人姓名和電話號碼,才可以成功進行修改,在數據庫中修改一句name字段值進行匹配

Android,手機通訊錄

Android,手機通訊錄

 

聯系人修改重點代碼:

//修改聯系人      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;

 

測試中的一些問題:聯系人為空時不能進行修改

Android,手機通訊錄

 

上述功能問題限制的重點代碼如下:

         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();    }

 

4. 刪除

刪除聯系人功能驗證:姓名:小 明 電話:15888899922

Android,手機通訊錄

Android,手機通訊錄

Android,手機通訊錄

 

聯系人刪除重點代碼:

//刪除聯系人       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;

 

測試中的一些問題:聯系人為空時不能進行刪除

Android,手機通訊錄

 

上述功能問題限制的重點代碼如下:

         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();    }


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日韩不卡在线 | 亚洲三级在线观看 | 草草影院浮力 | 99久久婷婷国产精品综合 | 亚洲精品动漫久久久久 | 麻豆一区二区 | 国产乱码精品一品二品 | 中文字幕 亚洲一区 | 69久久99精品久久久久婷婷 | 天天干天天躁 | 亚洲精品午夜电影 | 黄色毛片免费看 | 国产一级黄色大片 | 天天操天天干天天 | 成人精品一区二区三区中文字幕 | 日韩电影一区 | 中文字幕国产视频 | 九九久久九九 | 日韩福利在线 | 日韩免费一区二区 | 青草av在线 | 亚洲国产情侣自拍 | 九九热精品视频在线观看 | 91亚洲在线 | 亚洲成人在线网站 | 日韩欧美国产视频 | 欧美成人免费在线视频 | 欧美一区永久视频免费观看 | 你懂的在线视频播放 | www日 | 美国一级黄色片 | 久久99深爱久久99精品 | 久久久91精品国产一区二区精品 | 中文字幕在线乱码不卡二区区 | 日韩电影免费在线观看中文字幕 | 日韩欧美在线观看视频 | 日韩综合网 | 日韩艹逼视频 | 看片一区 | 国产精品久久国产精品99 gif | 青青免费视频 |