Android Studio实现一个简单的通讯录

Android Studio实现一个简单的通讯录,第1张

网上找的一个单页面通讯录,修改之后将添加联系人和修改/删除联系人分为两个独立页面

          

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控件设置监听
        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:  //添加联系人
                Intent intent=new Intent(MainActivity.this,nextActivity.class);
                startActivity(intent);
                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: //修改联系人
                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控件设置监听
        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:  //添加联系人
                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();
                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: //查询联系人
                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;
        }
    }
}

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



        
        
        

next.xml



    
        
        
    
    
    
    
    
    
    

xiugai.xml



    
        
        
    
    
        
        
    
    
        

Mainfest




    
        
            
                

                
            
        
        
        
    

初学android,程序还存在许多bug,轻喷

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/992049.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存