最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android個(gè)人手機(jī)通訊錄開發(fā)詳解

 更新時(shí)間:2019年02月14日 09:38:54   作者:Pretty_girll  
在本篇文章里小編給大家分享了關(guān)于Android個(gè)人手機(jī)通訊錄開發(fā)的步驟和相關(guān)源碼,有需要的朋友們學(xué)習(xí)下。

一、Android 個(gè)人手機(jī)通訊錄開發(fā)

數(shù)據(jù)存儲(chǔ):SQLite 數(shù)據(jù)庫(kù)

開發(fā)工具:Android Studio

二、Phone Module 簡(jiǎn)介

1. 界面展示

2. 文件結(jié)構(gòu)簡(jiǎn)單分析

三、個(gè)人手機(jī)通訊錄代碼實(shí)現(xiàn)

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控件設(shè)置監(jiān)聽
  btnQuery.setOnClickListener(this);
  btnUpdate.setOnClickListener(this);
  btnDelete.setOnClickListener(this);
  tvShow.setMovementMethod(ScrollingMovementMethod.getInstance()); //設(shè)置文本滾動(dòng)
 }
 @Override
 public void onClick(View v){
  String name;
  String phone;
  SQLiteDatabase db;
  switch (v.getId()){
   case R.id.btn_add:  //添加聯(lián)系人
    name = etName.getText().toString().trim();
    phone = etPhone.getText().toString().trim();
    db = myHelper.getWritableDatabase();
    if (name.equals("") || phone.equals("")){ //聯(lián)系人信息不能為空
     Toast.makeText(this,"聯(lián)系人信息添加失敗",Toast.LENGTH_SHORT).show();
    }
    else {
     db.execSQL("insert into person (name,phone) values(?,?)", new Object[]{name, phone});
     Toast.makeText(this,"聯(lián)系人信息添加成功",Toast.LENGTH_SHORT).show();
    }
    db.close();
    break;
   case R.id.btn_query: //查詢聯(lián)系人
    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: //修改聯(lián)系人
    db = myHelper.getWritableDatabase();
    name = etName.getText().toString().trim();
    phone = etPhone.getText().toString().trim();
    if (name.equals("") || phone.equals("")){ //聯(lián)系人信息不能為空
     Toast.makeText(this,"聯(lián)系人信息修改失敗",Toast.LENGTH_SHORT).show();
    }
    else {
     db.execSQL("update person set name=?,phone=? where name=?", new Object[]{name, phone, name});
     Toast.makeText(this,"聯(lián)系人信息修改成功",Toast.LENGTH_SHORT).show();
    }
    db.close();
    break;
   case R.id.btn_delete: //刪除聯(lián)系人
    db = myHelper.getWritableDatabase();
    name = etName.getText().toString().trim();
    phone = etPhone.getText().toString().trim();
    if (name.equals("") || phone.equals("")){ //聯(lián)系人信息不能為空
     Toast.makeText(this,"聯(lián)系人信息刪除失敗",Toast.LENGTH_SHORT).show();
    }
    else {
     db.execSQL("delete from person where name=? and phone=?", new Object[]{name, phone});
     Toast.makeText(this,"聯(lián)系人信息刪除成功",Toast.LENGTH_SHORT).show();
    }
    db.close();
    break;
  }
 }
}

3. MyHelper.java (數(shù)據(jù)庫(kù)文件)

/**
 * 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="  請(qǐng)輸入姓名"
   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="  請(qǐng)輸入手機(jī)號(hào)碼"
   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 按鈕設(shè)置)

<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">

 <!--設(shè)置背景色-->
 <solid android:color="#BC8F8F" />

 <!--設(shè)置圓角-->
 <corners android:radius="105dip" />

 <!--設(shè)置邊框線的寬度和顏色-->
 <stroke android:width="0dp" android:color="#B0C4DE" />
</shape>

四、Android 個(gè)人通訊錄功能測(cè)試

1. 添加

分別添加聯(lián)系人:姓名:小 明 電話:13888899922

         姓名:小 莉 電話:15866655588

添加聯(lián)系人功能驗(yàn)證:姓名:小 明 電話:13888899922

添加聯(lián)系人功能驗(yàn)證:姓名:小 莉 電話:15866655588

測(cè)試中的一些問題:1. 聯(lián)系人電話號(hào)碼不能重復(fù)添加,程序會(huì)終止退出,因?yàn)槁?lián)系人的電話號(hào)碼是唯一的(一個(gè)人可以有多個(gè)手機(jī)號(hào),而一個(gè)手機(jī)號(hào)只能一個(gè)人使用 {該功能程序已經(jīng)實(shí)現(xiàn)} )。

          2. 電話號(hào)碼長(zhǎng)度限制為11位。

          3. 聯(lián)系人信息為空不能成功添加。

再次添加聯(lián)系人:姓名:小 莉 電話:15866655588

上述功能問題限制的重點(diǎn)代碼如下:

//聯(lián)系人電話號(hào)碼唯一性

@Override

public void onCreate(SQLiteDatabase db){
  db.execSQL("create table person(id integer primary key autoincrement,name varchar(20),phone varchar(20) unique)");
}


//電話號(hào)碼長(zhǎng)度限制

<EditText
 android:id="@+id/et_phone"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:hint="  請(qǐng)輸入手機(jī)號(hào)碼"
 android:textSize="16dp"
 android:maxLength="11"/>


//聯(lián)系人信息為空時(shí)的限制

        case R.id.btn_add:  //添加聯(lián)系人
    name = etName.getText().toString().trim();
    phone = etPhone.getText().toString().trim();
    db = myHelper.getWritableDatabase();
    if (name.equals("") || phone.equals("")){ //聯(lián)系人信息不能為空
     Toast.makeText(this,"聯(lián)系人信息添加失敗",Toast.LENGTH_SHORT).show();
    }
    else {
     db.execSQL("insert into person (name,phone) values(?,?)", new Object[]{name, phone});
     Toast.makeText(this,"聯(lián)系人信息添加成功",Toast.LENGTH_SHORT).show();
    }
    db.close();
    break;

2. 查詢

查詢通訊錄聯(lián)系人功能驗(yàn)證:

聯(lián)系人查詢重點(diǎn)代碼:

//查詢聯(lián)系人

      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. 修改

修改聯(lián)系人功能驗(yàn)證:姓名:小 明 電話:13888899922 ===》》》 姓名:小 明 電話:15888899922

注意小問題:必須輸入聯(lián)系人姓名和電話號(hào)碼,才可以成功進(jìn)行修改,在數(shù)據(jù)庫(kù)中修改一句name字段值進(jìn)行匹配

聯(lián)系人修改重點(diǎn)代碼:

//修改聯(lián)系人

      case R.id.btn_update: 
    db = myHelper.getWritableDatabase();
    name = etName.getText().toString().trim();
    phone = etPhone.getText().toString().trim();
    if (name.equals("") || phone.equals("")){ //聯(lián)系人信息不能為空
     Toast.makeText(this,"聯(lián)系人信息修改失敗",Toast.LENGTH_SHORT).show();
    }
    else {
     db.execSQL("update person set name=?,phone=? where name=?", new Object[]{name, phone, name});
     Toast.makeText(this,"聯(lián)系人信息修改成功",Toast.LENGTH_SHORT).show();
    }
    db.close();
    break;

測(cè)試中的一些問題:聯(lián)系人為空時(shí)不能進(jìn)行修改

上述功能問題限制的重點(diǎn)代碼如下:

         if (name.equals("") || phone.equals("")){ //聯(lián)系人信息不能為空
     Toast.makeText(this,"聯(lián)系人信息修改失敗",Toast.LENGTH_SHORT).show();
    }
    else {
     db.execSQL("update person set name=?,phone=? where name=?", new Object[]{name, phone, name});
     Toast.makeText(this,"聯(lián)系人信息修改成功",Toast.LENGTH_SHORT).show();
    }

4. 刪除

刪除聯(lián)系人功能驗(yàn)證:姓名:小 明 電話:15888899922

聯(lián)系人刪除重點(diǎn)代碼:

//刪除聯(lián)系人

       case R.id.btn_delete: 
    db = myHelper.getWritableDatabase();
    name = etName.getText().toString().trim();
    phone = etPhone.getText().toString().trim();
    if (name.equals("") || phone.equals("")){ //聯(lián)系人信息不能為空
     Toast.makeText(this,"聯(lián)系人信息刪除失敗",Toast.LENGTH_SHORT).show();
    }
    else {
     db.execSQL("delete from person where name=? and phone=?", new Object[]{name, phone});
     Toast.makeText(this,"聯(lián)系人信息刪除成功",Toast.LENGTH_SHORT).show();
    }
    db.close();
    break;

測(cè)試中的一些問題:聯(lián)系人為空時(shí)不能進(jìn)行刪除

上述功能問題限制的重點(diǎn)代碼如下:

         if (name.equals("") || phone.equals("")){ //聯(lián)系人信息不能為空
     Toast.makeText(this,"聯(lián)系人信息刪除失敗",Toast.LENGTH_SHORT).show();
    }
    else {
     db.execSQL("delete from person where name=? and phone=?", new Object[]{name, phone});
     Toast.makeText(this,"聯(lián)系人信息刪除成功",Toast.LENGTH_SHORT).show();
    }

相關(guān)文章

  • Android 內(nèi)核代碼 wake_up源碼解析

    Android 內(nèi)核代碼 wake_up源碼解析

    這篇文章主要為大家介紹了Android 內(nèi)核代碼 wake_up源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • android實(shí)現(xiàn)Splash閃屏效果示例

    android實(shí)現(xiàn)Splash閃屏效果示例

    這篇文章主要介紹了android實(shí)現(xiàn)Splash閃屏效果的方法,涉及Android中postDelayed方法及AndroidManifest.xml權(quán)限控制的相關(guān)使用技巧,需要的朋友可以參考下
    2016-08-08
  • 基于移動(dòng)端真機(jī)調(diào)試的圖文教程(分享)

    基于移動(dòng)端真機(jī)調(diào)試的圖文教程(分享)

    下面小編就為大家分享一篇基于移動(dòng)端真機(jī)調(diào)試的圖文教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • Bitmap引起的OOM問題

    Bitmap引起的OOM問題

    這篇文章主要介紹了Bitmap引起的OOM問題,為什么會(huì)引起,以及避免引起的方法,文中有詳細(xì)的代碼示例,有需要的朋友可以參考下
    2023-04-04
  • Android使用廣播(BroadCast)實(shí)現(xiàn)強(qiáng)制下線的方法

    Android使用廣播(BroadCast)實(shí)現(xiàn)強(qiáng)制下線的方法

    這篇文章主要介紹了Android使用廣播(BroadCast)實(shí)現(xiàn)強(qiáng)制下線的方法,實(shí)例分析了Android廣播BroadCast控制activity關(guān)閉的具體步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2016-01-01
  • Android開發(fā)實(shí)現(xiàn)繪制淘寶收益圖折線效果示例

    Android開發(fā)實(shí)現(xiàn)繪制淘寶收益圖折線效果示例

    這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)繪制淘寶收益圖折線效果,涉及Android canvas圖形繪制及布局控制相關(guān)操作技巧,需要的朋友可以參考下
    2017-11-11
  • Android獲取app應(yīng)用程序大小的方法

    Android獲取app應(yīng)用程序大小的方法

    本文通過一段代碼給大家介紹android獲取app應(yīng)用程序大小的方法,由于android對(duì)這種方法進(jìn)行了封裝,我們沒有權(quán)限去調(diào)用這個(gè)方法,只能通過aidl,然后用java的反射機(jī)制去調(diào)用系統(tǒng)級(jí)方法,感興趣的朋友一起學(xué)習(xí)吧
    2015-11-11
  • Android實(shí)現(xiàn)隨意拖動(dòng)View效果的實(shí)例代碼

    Android實(shí)現(xiàn)隨意拖動(dòng)View效果的實(shí)例代碼

    這篇文章主要介紹了Android實(shí)現(xiàn)隨意拖動(dòng)View效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Android實(shí)現(xiàn)仿excel數(shù)據(jù)表格效果

    Android實(shí)現(xiàn)仿excel數(shù)據(jù)表格效果

    這篇文章主要介紹了Android實(shí)現(xiàn)仿excel數(shù)據(jù)表格效果的實(shí)現(xiàn)代碼,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-10-10
  • Android ExpandableListView使用方法案例詳解

    Android ExpandableListView使用方法案例詳解

    這篇文章主要介紹了Android ExpandableListView使用方法案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08

最新評(píng)論

沈阳市| 黄陵县| 承德县| 北安市| 凤阳县| 榆树市| 浮山县| 富平县| 高唐县| 宝清县| 沁水县| 达孜县| 桂平市| 合肥市| 福泉市| 会同县| 拉萨市| 建昌县| 鄂尔多斯市| 玉门市| 开化县| 南靖县| 页游| 苍南县| 平顶山市| 包头市| 鞍山市| 冀州市| 英超| 安平县| 阿拉善左旗| 杂多县| 巨鹿县| 祁连县| 金阳县| 宜春市| 永年县| 峨眉山市| 罗江县| 平谷区| 衡阳县|