av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:10日期:2022-09-26 11:27:41

本文實(shí)例為大家分享了Android通訊錄案例,供大家參考,具體內(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è)類繼承SQLiteOpenHelper② 重寫父類構(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' > <ImageViewandroid:layout_width='160dp'android:layout_height='120dp'android:layout_marginTop='50dp'android:layout_marginLeft='20dp'android:src='http://m.4tl426be.cn/bcjs/@drawable/expression'></ImageView> <ImageViewandroid:layout_width='160dp'android:layout_height='120dp'android:layout_marginTop='50dp'android:layout_marginLeft='20dp'android:src='http://m.4tl426be.cn/bcjs/@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' > <TextViewandroid: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' /> <EditTextandroid: 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' > <TextViewandroid: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' /> <EditTextandroid: 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' > <Buttonandroid: android:layout_width='0dp'android:layout_weight='1'android:layout_height='wrap_content'android:text='增加'android:textSize='26sp'></Button> <Buttonandroid: android:layout_width='0dp'android:layout_weight='1'android:layout_height='wrap_content'android:text='查詢'android:textSize='26sp'></Button> <Buttonandroid: android:layout_width='0dp'android:layout_weight='1'android:layout_height='wrap_content'android:text='修改'android:textSize='26sp'></Button> <Buttonandroid: android:layout_width='0dp'android:layout_weight='1'android:layout_height='wrap_content'android:text='刪除'android:textSize='26sp'></Button> </LinearLayout> <TextView android: 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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 久久国产欧美日韩精品 | 日本不卡一区 | 色视频在线播放 | 精品久久一区 | 精品一区二区三区四区 | 粉嫩粉嫩芽的虎白女18在线视频 | 欧美精品久久久 | 麻豆av在线免费观看 | 日韩精品成人网 | 亚洲人成免费 | 国产精品色哟哟网站 | 最新黄色在线观看 | 毛片高清 | 中文字幕精品视频 | 国产露脸对白88av | 欧美日韩亚洲一区 | 国产色在线 | 国产女人与拘做视频免费 | 久久精品国产精品青草 | 亚洲精品在线视频 | 国产精品久久久久久久久久免费看 | 中国免费黄色片 | 亚洲国产成人精品女人久久久野战 | 美女视频. | 毛片在线视频 | 天天综合永久入口 | 91av视频| 精品福利在线视频 | 国产在线播放av | www亚洲一区 | 午夜欧美一区二区三区在线播放 | 日韩中文字幕在线视频 | 日韩精品一区二区三区在线播放 | 日韩视频国产 | 91精品久久久久久久久久 | 亚洲网站在线观看 | 国产午夜精品一区二区三区 | 日本一区精品 | 日韩中文在线观看 | 黄色一级电影在线观看 | 男女午夜激情视频 |