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

Android Studio實現(xiàn)簡單的通訊錄

 更新時間:2021年04月26日 17:01:06   作者:諸葛琴魔q  
這篇文章主要為大家詳細介紹了Android Studio實現(xiàn)簡單的通訊錄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

網(wǎng)上找的一個單頁面通訊錄,修改之后將添加聯(lián)系人和修改/刪除聯(lián)系人分為兩個獨立頁面

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控件設置監(jiān)聽
        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:  //添加聯(lián)系人
                Intent intent=new Intent(MainActivity.this,nextActivity.class);
                startActivity(intent);
                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,"當前無聯(lián)系人",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)系人
                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控件設置監(jiān)聽
        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:  //添加聯(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();
                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: //查詢聯(lián)系人
                db = myHelper.getReadableDatabase();
                Cursor cursor = db.rawQuery("select name,phone from person",null);
                if (cursor.getCount() == 0){
                    tvShow.setText("");
                    Toast.makeText(this,"當前無聯(lián)系人",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;
        }
    }
}

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=" 添加聯(lián)系人"
            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="查看聯(lián)系人"
            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=" 修改聯(lián)系人"
            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="查看聯(lián)系人"
            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,大家多提修改建議。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Android性能優(yōu)化getResources()與Binder導致界面卡頓優(yōu)化

    Android性能優(yōu)化getResources()與Binder導致界面卡頓優(yōu)化

    這篇文章主要為大家介紹了Android性能優(yōu)化getResources()與Binder導致界面卡頓優(yōu)化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • Flutter實現(xiàn)自定義搜索框AppBar的示例代碼

    Flutter實現(xiàn)自定義搜索框AppBar的示例代碼

    開發(fā)中,頁面頭部為搜索樣式的設計非常常見,為了可以像系統(tǒng)AppBar那樣使用,本文將利用Flutter自定義一個搜索框,感興趣的可以了解一下
    2022-04-04
  • 三種Android單擊事件onclick的實現(xiàn)方法

    三種Android單擊事件onclick的實現(xiàn)方法

    這篇文章主要為大家詳細介紹了三種Android單擊事件onclick的實現(xiàn)方法,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android自定義驗證碼輸入框的方法實例

    Android自定義驗證碼輸入框的方法實例

    這篇文章主要給大家介紹了關于Android自定義驗證碼輸入框的相關資料,文中通過實例代碼介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學習價值,需要的朋友可以參考下
    2022-02-02
  • Android實現(xiàn)可以展開的TextView

    Android實現(xiàn)可以展開的TextView

    這篇文章主要為大家詳細介紹了Android實現(xiàn)可以展開的TextView,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android開發(fā)中Intent.Action各種常見的作用匯總

    Android開發(fā)中Intent.Action各種常見的作用匯總

    今天小編就為大家分享一篇關于Android開發(fā)中Intent.Action各種常見的作用匯總,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 詳解Android 中AsyncTask 的使用

    詳解Android 中AsyncTask 的使用

    這篇文章主要介紹了詳解Android 中AsyncTask 的使用 的相關資料,這里提供實例幫助大家學習理解這部分內容,需要的朋友可以參考下
    2017-09-09
  • Android getSystemService用法實例總結

    Android getSystemService用法實例總結

    這篇文章主要介紹了Android getSystemService用法,結合實例形式總結分析了getSystemService獲取系統(tǒng)Service的相關使用方法與注意事項,需要的朋友可以參考下
    2016-01-01
  • Android SurfaceView基礎用法詳解

    Android SurfaceView基礎用法詳解

    這篇文章主要介紹了Android SurfaceView基礎用法詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-08-08
  • Android控件之CheckBox、RadioButton用法實例分析

    Android控件之CheckBox、RadioButton用法實例分析

    這篇文章主要介紹了Android控件之CheckBox、RadioButton用法,以實例形式較為詳細的分析了CheckBox和RadioButton實現(xiàn)復選按鈕及單選按鈕功能的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09

最新評論

涞水县| 永丰县| 宜章县| 万荣县| 阳东县| 景洪市| 富平县| 富蕴县| 中宁县| 沙湾县| 类乌齐县| 安塞县| 嘉峪关市| 墨脱县| 始兴县| 仙居县| 武夷山市| 建湖县| 娱乐| 姚安县| 禹城市| 松原市| 辽源市| 察哈| 玛多县| 东兰县| 缙云县| 彭水| 榕江县| 额济纳旗| 合作市| 上高县| 阿图什市| 麻城市| 朝阳区| 东安县| 台北市| 广水市| 拉孜县| 淳安县| 中山市|