寒假福利5
项目源码
MainActivity.java
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import pl.com.salsoft.sqlitestudioremote.SQLiteStudioService; public class MainActivity extends Activity { private Button mBtnLogin; private EditText mEtUserName; private Button mBtnRegister; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBtnLogin= findViewById(R.id.btn_login); mBtnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //跳转到主页面 Intent intent = new Intent(MainActivity.this, ZhuActivity.class); startActivity(intent); } }); mEtUserName = findViewById(R.id.et_1); mEtUserName.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Log.d("edittext",s.toString()); } @Override public void afterTextChanged(Editable s) { } }); mBtnRegister= findViewById(R.id.btn_register); mBtnRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //跳转到register页面 Intent intent = new Intent(MainActivity.this, RegisterActivity.class); startActivity(intent); } }); } }
RegisterActivity.java
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import pl.com.salsoft.sqlitestudioremote.SQLiteStudioService; public class RegisterActivity extends AppCompatActivity { private Button rgt_3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); rgt_3 = findViewById(R.id.rgt_3); rgt_3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show(); //跳转到起始页面 Intent intent = new Intent(RegisterActivity.this, MainActivity.class); startActivity(intent); } }); } }
ZhuActivity.java
package com.example.myapplication; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; 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 com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.MapStatusUpdate; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.model.LatLng; import java.text.SimpleDateFormat; import java.util.Date; public class ZhuActivity extends AppCompatActivity { private EditText zDate; //日期 private EditText time; //时间 private EditText address; //地址 private Button tijiao; //提交按钮 LocationClient mLocationClient; //定位客户端 private MapView mMapView; //声明地图组件 private BaiduMap mBaiduMap; //定义百度地图对象 private boolean isFirstLoc; //记录是否是第一次定位 /* private Calendar calendar; // 通过Calendar获取系统时间 private int mYear; private int mMonth; private int mDay; private LinearLayout layoutDate;*/ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //如果没有定位权限,动态请求用户允许使用该权限 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); }else { requestLocation(); } //日期 zDate = findViewById(R.id.zhu_2); SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy年MM月dd日 "); Date date1 = new Date(System.currentTimeMillis()); zDate.setText(simpleDateFormat1.format(date1)); /* 日历 zDate = findViewById(R.id.zhu_2); calendar = Calendar.getInstance(); // 点击"日期"按钮布局 设置日期 zDate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new DatePickerDialog(ZhuActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int day) { // TODO Auto-generated method stub mYear = year; mMonth = month; mDay = day; // 更新EditText控件日期 小于10加0 zDate.setText(new StringBuilder() .append(mYear) .append("-") .append((mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1)) .append("-") .append((mDay < 10) ? "0" + mDay : mDay)); } }, calendar.get(Calendar.YEAR), calendar .get(Calendar.MONTH), calendar .get(Calendar.DAY_OF_MONTH)).show(); } }); */ //提交 tijiao = findViewById(R.id.tijiao); tijiao.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(ZhuActivity.this, "提交成功", Toast.LENGTH_SHORT).show(); //跳转到成功页面 Intent intent = new Intent(ZhuActivity.this, ShowActivity.class); startActivity(intent); } }); //时间 time = findViewById(R.id.time); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss"); Date date = new Date(System.currentTimeMillis()); time.setText(simpleDateFormat.format(date)); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case 1: if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "没有定位权限!", Toast.LENGTH_LONG).show(); finish(); } else { requestLocation(); } } } private void requestLocation() { initLocation(); mLocationClient.start(); } private void initLocation() { //初始化 mLocationClient = new LocationClient(getApplicationContext()); mLocationClient.registerLocationListener(new MyLocationListener()); SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_zhu); mMapView = findViewById(R.id.baiDuMap); mBaiduMap = mMapView.getMap(); address = findViewById(R.id.zhu_4); LocationClientOption option = new LocationClientOption(); //设置扫描时间间隔 option.setScanSpan(1000); //设置定位模式,三选一 option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy); /*option.setLocationMode(LocationClientOption.LocationMode.Battery_Saving); option.setLocationMode(LocationClientOption.LocationMode.Device_Sensors);*/ //设置需要地址信息 option.setIsNeedAddress(true); //保存定位参数 mLocationClient.setLocOption(option); } //内部类,百度位置监听器 private class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation bdLocation) { address.setText(bdLocation.getAddrStr()); if(bdLocation.getLocType()==BDLocation.TypeGpsLocation || bdLocation.getLocType()==BDLocation.TypeNetWorkLocation){ navigateTo(bdLocation); } } } private void navigateTo(BDLocation bdLocation) { if(isFirstLoc){ LatLng ll = new LatLng(bdLocation.getLatitude(),bdLocation.getLongitude()); MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll); mBaiduMap.animateMapStatus(update); isFirstLoc = false; } } @Override protected void onResume() { super.onResume(); mMapView.onResume(); } @Override protected void onPause() { super.onPause(); mMapView=findViewById(R.id.baiDuMap); mMapView.onResume(); } @Override protected void onDestroy() { super.onDestroy(); mLocationClient.stop(); mMapView.onDestroy(); } }