Android怎么實(shí)現(xiàn)通訊錄功能

這篇文章給大家分享的是有關(guān)Android怎么實(shí)現(xiàn)通訊錄功能的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專(zhuān)注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、重慶小程序開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶(hù)創(chuàng)新互聯(lián)還提供了雙湖免費(fèi)建站歡迎大家使用!

Android是什么

Android是一種基于Linux內(nèi)核的自由及開(kāi)放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開(kāi)放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開(kāi)發(fā)。

具體內(nèi)容如下

實(shí)戰(zhàn)演練——通訊錄

1、功能描述:通過(guò)SQLite實(shí)現(xiàn)數(shù)據(jù)庫(kù)的增刪改查

2、技術(shù)要點(diǎn):SQLite的基本操作

3、實(shí)現(xiàn)步驟:

① 創(chuàng)建一個(gè)類(lèi)繼承SQLiteOpenHelper
② 重寫(xiě)父類(lèi)構(gòu)造方法、onCreate()、onUpgrade()
③ 增刪改查

4、效果圖

Android怎么實(shí)現(xiàn)通訊錄功能

5、案例代碼

MyHelper.java

package com.example.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class MyHelper extends SQLiteOpenHelper {
  public MyHelper(@Nullable Context context) {
    super(context, "test.db", null, 1);
  }

  //當(dāng)數(shù)據(jù)庫(kù)第一次創(chuàng)建的時(shí)候執(zhí)行
  @Override
  public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT ,name VARCHAR(20),phone VARCHAR(20))");
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  }
}

MainActivity.java

package com.example.sqlite;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  private TextView name;
  private TextView phone;
  private Button btnAdd;
  private Button btnDel;
  private Button btnUqd;
  private Button btnSel;
  private String uPhone;
  private String uName;
  private MyHelper myHelper;
  private SQLiteDatabase db;
  private TextView show;
  private ContentValues contentValues;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myHelper = new MyHelper(this);
    init();
  }

  private void init() {
    show = findViewById(R.id.show);
    name = findViewById(R.id.name);
    phone = findViewById(R.id.phone);
    btnAdd = findViewById(R.id.insert);
    btnDel = findViewById(R.id.delete);
    btnUqd = findViewById(R.id.update);
    btnSel = findViewById(R.id.select);
    btnAdd.setOnClickListener(this);
    btnDel.setOnClickListener(this);
    btnUqd.setOnClickListener(this);
    btnSel.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.select:
        db = myHelper.getReadableDatabase();
        Cursor cursor = db.query("information", null, null, null, null, null, null);
        if (cursor.getCount() == 0) {
          Toast.makeText(this, "沒(méi)有數(shù)據(jù)", Toast.LENGTH_LONG).show();
        } else {
          cursor.moveToFirst();
          show.setText("Name:" + cursor.getString(1) + "Tel:" + cursor.getString(2));
        }
        while (cursor.moveToNext()) {
          show.append("\n" + "Name" + cursor.getString(1) + "Tel" + cursor.getString(2));
        }
        cursor.close();
        db.close();
        break;
      case R.id.insert:
        uName = name.getText().toString();
        uPhone = phone.getText().toString();
        db = myHelper.getReadableDatabase();
        contentValues = new ContentValues();
        contentValues.put("name", uName);
        contentValues.put("phone", uPhone);
        db.insert("information", null, contentValues);
        db.close();
        break;
      case R.id.update:
        db = myHelper.getReadableDatabase();
        contentValues = new ContentValues();
        contentValues.put("phone", uPhone = phone.getText().toString());
        db.update("information", contentValues, "name=?", new String[]{name.getText().toString()});
        db.close();
        break;
      case R.id.delete:
        db = myHelper.getReadableDatabase();
        db.delete("information", null, null);
        Toast.makeText(this, "信息已經(jīng)刪除", Toast.LENGTH_LONG).show();
        show.setText("");
        db.close();
        break;
    }
  }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity"
  android:background="@drawable/background">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      >

      <ImageView
        android:layout_width="160dp"
        android:layout_height="120dp"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="20dp"
        android:src="@drawable/expression"></ImageView>
      <ImageView
        android:layout_width="160dp"
        android:layout_height="120dp"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="20dp"
        android:src="@drawable/text"></ImageView>
    </LinearLayout>
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:layout_marginTop="20dp"
      android:paddingHorizontal="20dp"
      >
      <TextView

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="姓 名 :"
        android:textSize="26sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
      <EditText
        android:id="@+id/name"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="wrap_content"
        android:hint="請(qǐng)輸入姓名"
        android:textSize="22sp"
        ></EditText>

    </LinearLayout>
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:layout_marginTop="20dp"
      android:paddingHorizontal="20dp"
      >
      <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="電 話(huà) :"
        android:textSize="26sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
      <EditText
        android:id="@+id/phone"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="wrap_content"
        android:hint="請(qǐng)輸入手機(jī)號(hào)碼"
        android:textSize="22sp"
        ></EditText>

    </LinearLayout>
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:layout_marginTop="20dp"
      android:paddingHorizontal="20dp"
      >
      <Button
        android:id="@+id/insert"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="增加"
        android:textSize="26sp"
        ></Button>
      <Button
        android:id="@+id/select"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="查詢(xún)"
        android:textSize="26sp"
        ></Button>
      <Button
        android:id="@+id/update"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="修改"
        android:textSize="26sp"
        ></Button>
      <Button
        android:id="@+id/delete"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="刪除"
        android:textSize="26sp"
        ></Button>
    </LinearLayout>
    <TextView
      android:id="@+id/show"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:textSize="18sp"
      android:background="#80ffffff"
      android:layout_marginHorizontal="20dp"

      ></TextView>
  </LinearLayout>
</RelativeLayout>

感謝各位的閱讀!關(guān)于“Android怎么實(shí)現(xiàn)通訊錄功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

本文名稱(chēng):Android怎么實(shí)現(xiàn)通訊錄功能
轉(zhuǎn)載注明:http://bm7419.com/article44/gocoee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、網(wǎng)站收錄、品牌網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站網(wǎng)站營(yíng)銷(xiāo)、自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)