主要代码:
MainActivity.java
package com.example.helloworld; import androidx.appcompat.app.AppCompatActivity; import android.app.AlertDialog; import android.content.ContentValues; import android.content.Context; import android.content.DialogInterface; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import com.baidu.location.BDAbstractLocationListener; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.mapapi.SDKInitializer; import java.security.KeyPairGenerator; import java.text.SimpleDateFormat; import java.util.Date; public class MainActivity extends AppCompatActivity { private EditText text1; private EditText text2; private EditText text3; private EditText text4; private WenDate wendate=new WenDate(); private TextView mTv = null; public LocationClient mLocationClient = null; private MyLocationListener myListener = new MyLocationListener(); public class MyLocationListener extends BDAbstractLocationListener { @Override public void onReceiveLocation(BDLocation location){ //此处的BDLocation为定位结果信息类,通过它的各种get方法可获取定位相关的全部结果 //以下只列举部分获取地址相关的结果信息 //更多结果信息获取说明,请参照类参考中BDLocation类中的说明 String addr = location.getAddrStr(); //获取详细地址信息 String country = location.getCountry(); //获取国家 String province = location.getProvince(); //获取省份 String city = location.getCity(); //获取城市 String district = location.getDistrict(); //获取区县 String street = location.getStreet(); //获取街道信息 String adcode = location.getAdCode(); //获取adcode String town = location.getTown(); //获取乡镇信息 text4=(EditText)findViewById(R.id.tv_text4); text4.setText(country+province+city+district+town+street+adcode); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v("MainActivity","MainActivity启动"); // MyDBHelper mydbh=new MyDBHelper(this,"mydb.db",1); //SQLiteDatabase sqldb=mydbh.getReadableDatabase(); SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); } public void autoAddress(View view) { mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类 mLocationClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setIsNeedAddress(true); option.setNeedNewVersionRgc(true); mLocationClient.setLocOption(option); //注册监听函数 mLocationClient.start(); } public void autoTimeAndDate(View view) { text2=(EditText)findViewById(R.id.tv_text2); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss //获取当前时间 Date date = new Date(System.currentTimeMillis()); text2.setText(simpleDateFormat.format(date)); } public void createDB(View view) { MyDBHelper mydbh=new MyDBHelper(this,"amydb.db",1); SQLiteDatabase sqldb=mydbh.getReadableDatabase(); } public void insertDB(View view) { MyDBHelper mydbh=new MyDBHelper(this,"amydb.db",1); SQLiteDatabase sqldb=mydbh.getReadableDatabase(); ContentValues contentvalues=new ContentValues(); text1=(EditText)findViewById(R.id.tv_text1); text2=(EditText)findViewById(R.id.tv_text2); text3=(EditText)findViewById(R.id.tv_text3); text4=(EditText)findViewById(R.id.tv_text4); contentvalues.put("name",text1.getText().toString()); contentvalues.put("dateandtime",text2.getText().toString()); contentvalues.put("address",text4.getText().toString()); contentvalues.put("wendu",text3.getText().toString()); long flag=sqldb.insert("personwendu",null,contentvalues); Toast.makeText(this,"数据加入成功"+flag,Toast.LENGTH_LONG).show(); } public void queryData(View view) { MyDBHelper mydbh=new MyDBHelper(this,"amydb.db",1); SQLiteDatabase sqldb=mydbh.getReadableDatabase(); Cursor cursor=sqldb.rawQuery("select * from personwendu",null); String str=""; if(cursor.moveToFirst()) { do{ String name=cursor.getString(cursor.getColumnIndex("name")); String dateandtime=cursor.getString(cursor.getColumnIndex("dateandtime")); String address=cursor.getString(cursor.getColumnIndex("address")); String wendu=cursor.getString(cursor.getColumnIndex("wendu")); str=str+name+"|"+dateandtime+"|"+wendu+" "+address+" "; }while(cursor.moveToNext()); } AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("历史记录").setIcon(R.mipmap.ic_launcher).setMessage(str) .setPositiveButton("ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); AlertDialog ad=builder.create(); ad.show(); } public void updateData(View view) { MyDBHelper mydbh=new MyDBHelper(this,"mydb.db",1); SQLiteDatabase sqldb=mydbh.getReadableDatabase(); ContentValues contentvalues=new ContentValues(); contentvalues.put("name","明明"); contentvalues.put("age","12"); int flag=sqldb.update("person",contentvalues,"name=?",new String[]{"李明"}); Toast.makeText(this,"已有"+flag+"条数据修改",Toast.LENGTH_LONG).show(); } public void deleteDate(View view) { MyDBHelper mydbh=new MyDBHelper(this,"mydb.db",1); SQLiteDatabase sqldb=mydbh.getReadableDatabase(); int flag=sqldb.delete("person","id=1",null); Toast.makeText(this,"已有"+flag+"条数据删除",Toast.LENGTH_LONG).show(); } }
MyDBHelper.java
package com.example.helloworld; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.widget.Toast; import androidx.annotation.Nullable; public class MyDBHelper extends SQLiteOpenHelper { private Context context; public MyDBHelper(Context context,String name,int version) { super(context, name, null, version); this.context=context; } @Override public void onCreate(SQLiteDatabase db) { String sql="create table personwendu(id integer primary key autoincrement, address varchar, name varchar, wendu varchar, dateandtime varchar)"; db.execSQL(sql); Toast.makeText(context,"数据加入成",Toast.LENGTH_LONG).show(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
SecondActivity.java
package com.example.helloworld; import android.os.Bundle; import android.util.Log; import androidx.appcompat.app.AppCompatActivity; public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v("SecondActivity","SecondActivity启动"); setContentView(R.layout.activity_second); } }
WenData.java
package com.example.helloworld; public class WenDate { private String name; private String dateandtime; private String address; private String wendu; public String getWendu() { return wendu; } public void setWendu(String wendu) { this.wendu = wendu; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDateandtime() { return dateandtime; } public void setDateandtime(String dateandtime) { this.dateandtime = dateandtime; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public WenDate() { } public WenDate(String name,String dateandtime,String address,String wendu) { this.name=name; this.dateandtime=dateandtime; this.address=address; this.wendu=wendu; } }
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" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFCCF5" /> <EditText android:id="@+id/tv_text1" android:layout_width="300dp" android:layout_height="66dp" android:layout_above="@+id/tv_button1" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_marginEnd="7dp" android:layout_marginRight="7dp" android:layout_marginBottom="338dp" android:hint="请输入姓名" android:textColorHint="#cc00" /> <EditText android:id="@+id/tv_text2" android:layout_width="300dp" android:layout_height="66dp" android:layout_above="@+id/tv_button1" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_marginEnd="11dp" android:layout_marginRight="11dp" android:layout_marginBottom="273dp" android:hint="点击以获取当前时间与日期" android:onClick="autoTimeAndDate" android:textColorHint="#cc00" /> <EditText android:id="@+id/tv_text3" android:layout_width="300dp" android:layout_height="66dp" android:layout_above="@+id/tv_button1" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_marginEnd="11dp" android:layout_marginRight="11dp" android:layout_marginBottom="222dp" android:hint="请输入现在的体温" android:textColorHint="#cc00" /> <EditText android:id="@+id/tv_text4" android:layout_width="300dp" android:layout_height="66dp" android:layout_above="@+id/tv_button1" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_marginEnd="11dp" android:layout_marginRight="11dp" android:layout_marginBottom="164dp" android:hint="点击以获取当前位置" android:textColorHint="#cc00" android:onClick="autoAddress"/> <Button android:id="@+id/tv_button1" android:layout_width="252dp" android:layout_height="66dp" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:layout_centerVertical="true" android:layout_marginEnd="44dp" android:layout_marginRight="44dp" android:layout_marginBottom="131dp" android:background="#66ffff" android:onClick="insertDB" android:text="确定添加" android:textColor="#cc00" android:textSize="20sp" /> <Button android:id="@+id/tv_button2" android:layout_width="252dp" android:layout_height="66dp" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:layout_centerVertical="true" android:layout_marginEnd="50dp" android:layout_marginRight="44dp" android:layout_marginBottom="46dp" android:background="#66ffff" android:onClick="queryData" android:text="查看历史记录" android:textColor="#cc00" android:textSize="20sp" /> </RelativeLayout>
activity_second.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="171dp" android:layout_marginLeft="171dp" android:layout_marginTop="356dp" android:layout_marginEnd="212dp" android:layout_marginRight="212dp" android:layout_marginBottom="356dp" android:rotation="24" android:rotationX="174" android:rotationY="-137" android:text="第二个页面" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>