3.8
花费时间 4小时
代码量 450行
博客量 1篇
学习知识点 安卓开发
//activity.xml
其中的android:background="@drawable/gb3"可以根据自己的需要更改背景
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="@drawable/gb3"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:layout_gravity="right"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="#B916D5"
android:textStyle="bold"
android:textSize="110sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="19sp"
android:text="姓名:"
android:textColor="#B916D5"/>
<EditText android:id="@+id/et_uname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="19sp"
android:hint="请输入姓名信息"
android:textColor="@color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="19sp"
android:text="手机:"
android:textColor="#B916D5"/>
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入手机信息"
android:textColor="#B916D5"
android:textSize="19sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="horizontal">
<Button android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="添加"/>
<Button android:id="@+id/btn_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="修改"/>
<Button android:id="@+id/btn_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="删除"/>
<Button android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="清空"/>
<Button android:id="@+id/btn_query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="查询"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="206dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="120dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="联系人信息如下:"
android:textColor="#B916D5" />
<TextView
android:id="@+id/tv_contacts"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>
然后是MainActivity中的
package com.example.demo1;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText et_uname,et_phone;//姓名和手机控件变量
private Button btn_add;
private Button btn_edit;
private Button btn_del;
private Button btn_clear;
private Button btn_query;
private TextView tv_title,tv_contacts;//显示信息的文本框
//数据库操作相关的成员变量
private MyDbHelper myDbHelper;//数据库帮助工具类
private SQLiteDatabase db;//数据库类
private ContentValues values;//数据表的操作参数
private static final String mTableName = "contacts";
// 此处重写了父类中的onCreate方法,用@override判断是否正确重写
@Override
protected void onCreate(Bundle savedInstanceState) {
// savedInstanceState的作用是保存activity的状态,即用户通过home键或android意外回收进程时,保存此时用户的状态
super.onCreate(savedInstanceState);
// 与布局文件建立联系,
setContentView(R.layout.activity_main);
initView();
queryData();
}
//初始化界面控件
private void initView(){
et_uname = findViewById(R.id.et_uname);
et_phone = findViewById(R.id.et_phone);
tv_title = findViewById(R.id.tv_title);
tv_contacts = findViewById(R.id.tv_contacts);
btn_add = findViewById(R.id.btn_add);
btn_edit = findViewById(R.id.btn_edit);
btn_del = findViewById(R.id.btn_del);
btn_clear = findViewById(R.id.btn_clear);
btn_query = findViewById(R.id.btn_query);
//myDbHelper = new MyDbHelper(this);//初始化数据库工具类
myDbHelper = new MyDbHelper(this,"mycontacts.db",null,2);
btn_add.setOnClickListener(this);
btn_edit.setOnClickListener(this);
btn_del.setOnClickListener(this);
btn_clear.setOnClickListener(this);
btn_query.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String uname = et_uname.getText().toString().trim();
String phone = et_phone.getText().toString().trim();
if(TextUtils.isEmpty(uname)){
showMsg("请输入姓名信息");
}else if(TextUtils.isEmpty(phone)){
showMsg("请输入电话信息");
}else{
if (v.getId()==R.id.btn_add) { addData(uname,phone);}
if (v.getId()==R.id.btn_edit) { editData(uname,phone);}
if (v.getId()==R.id.btn_del) { delData(uname);}
if (v.getId()==R.id.btn_clear) { clearData();}
if (v.getId()==R.id.btn_query) { queryData();}
}
}
private void showMsg(String msg){
Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
}
private void addData(String uname,String phone){
db = myDbHelper.getWritableDatabase();
values = new ContentValues();
values.put("uname",uname);
values.put("phone",phone);
db.insert(mTableName,null,values);
showMsg("成功添加一条联系人信息");
queryData();
}
private void editData(String uname,String phone){
db = myDbHelper.getWritableDatabase();
values = new ContentValues();
values.put("uname",uname);
values.put("phone",phone);
db.update(mTableName,values,"uname=?",new String[]{uname});
showMsg("成功修改一条联系人信息");
queryData();
}
private void delData(String uname){
db = myDbHelper.getWritableDatabase();
db.delete(mTableName,"uname=?",new String[]{uname});
showMsg("成功的删除一条联系人信息");
queryData();
}
private void clearData(){
db = myDbHelper.getWritableDatabase();
db.delete(mTableName,null,null);
showMsg("成功清空所有联系人信息");
queryData();
}
private void queryData(){
db = myDbHelper.getReadableDatabase();
Cursor cursor = db.query(mTableName,null,null,null,null,null,null);
if(cursor.getCount() == 0){
tv_title.setText("当前没有联系人数据");
tv_contacts.setText("");
}else{
tv_title.setText("当前有【"+cursor.getCount()+"】个联系人,信息如下:");
String strResult = "";
while(cursor.moveToNext()){
strResult+= cursor.getInt(0);
strResult+= ". 姓名:" + cursor.getString(1);
strResult+= ". 手机:" + cursor.getString(2);
strResult+= "\n";
}
tv_contacts.setText(strResult);
}
}
class MyDbHelper extends SQLiteOpenHelper{
public MyDbHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, "mycontacts.db", null, 2);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + mTableName + "(_id integer primary key autoincrement, "+
"uname varchar(50) unique, phone varchar (13))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}