1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| package cn.n1ng.addressbook;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint; import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private MyHelper myHelper; private EditText et_name; private EditText et_phone; private Button btn_add; private Button btn_del; private Button btn_update; private Button btn_get; private Button btn_call; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myHelper = new MyHelper(this); init(); }
private void dialPhoneNumber(String phoneNumber){ Intent intent = new Intent(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:"+phoneNumber)); startActivity(intent); } private void init() { et_name = (EditText) findViewById(R.id.et_name); et_phone = (EditText) findViewById(R.id.et_phone); btn_add = (Button) findViewById(R.id.btn_add); btn_del = (Button) findViewById(R.id.btn_del); btn_update = (Button) findViewById(R.id.btn_update); btn_get = (Button) findViewById(R.id.btn_get); btn_call = (Button) findViewById(R.id.btn_call); btn_add.setOnClickListener(this); btn_del.setOnClickListener(this); btn_update.setOnClickListener(this); btn_get.setOnClickListener(this); btn_call.setOnClickListener(this); }
@SuppressLint("Range") @Override public void onClick(View view) { String name ; String phone; SQLiteDatabase db; ContentValues values; switch (view.getId()){ case R.id.btn_add: db = myHelper.getWritableDatabase(); name = et_name.getText().toString(); phone = et_phone.getText().toString(); values = new ContentValues(); values.put("name",name); values.put("phone",phone); db.insert("information",null,values); Toast.makeText(this,"添加成功", Toast.LENGTH_SHORT).show(); db.close(); et_phone.setText(""); et_name.setText(""); break; case R.id.btn_del: db = myHelper.getWritableDatabase(); name = et_name.getText().toString(); db.delete("information","name = ?",new String[]{name}); Toast.makeText(this,"删除成功", Toast.LENGTH_SHORT).show(); db.close(); et_phone.setText(""); et_name.setText(""); break; case R.id.btn_update: db = myHelper.getWritableDatabase(); name = et_name.getText().toString(); phone = et_phone.getText().toString(); values = new ContentValues(); values.put("phone",phone); db.update("information",values,"where name = ?",new String[]{name}); Toast.makeText(this,"更新成功", Toast.LENGTH_SHORT).show(); db.close(); et_phone.setText(""); et_name.setText(""); break; case R.id.btn_get: db = myHelper.getWritableDatabase(); name = et_name.getText().toString(); Cursor cursor = db.query("information",null,null,null,null,null,null); if(cursor.getCount()==0){ Toast.makeText(this,"没有数据", Toast.LENGTH_SHORT).show(); } while (cursor.moveToNext()){ if(name.equals(cursor.getString(cursor.getColumnIndex("name")))){ et_phone.setText(cursor.getString(cursor.getColumnIndex("phone"))); Toast.makeText(this,"查找成功", Toast.LENGTH_SHORT).show(); } } break; case R.id.btn_call: phone = et_phone.getText().toString(); dialPhoneNumber(phone); break; } } class MyHelper extends SQLiteOpenHelper{
public MyHelper(Context context) { super(context, "information", null, 1); }
@Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE information(_id integer primary key,name varchar(20),phone varchar(20))"); }
@Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
} } }
|