功能介绍
先展示效果图:
侧边栏由MaterialDesign(原质化设计)框架设计而成。
由DrawerLayout+NavigationView组成
需要导入的包如下:
其中circleimageview是将图片放入圆型容器之中
implementation 'com.google.android.material:material:1.1.0-alpha09' implementation 'com.getbase:floatingactionbutton:1.10.1' implementation 'com.google.android.material:material:1.0.0' implementation 'de.hdodenhof:circleimageview:2.1.0'
代码详解
布局文件代码如下:
app:headerLayout:代表头部文件内容
app:menu:代表下面菜单内容
android:layout_gravity=“left” 为侧边栏所在位置以及滑动方向
<com.google.android.material.navigation.NavigationView android:id="@+id/InformationManage" android:layout_width="match_parent" android:layout_height="match_parent" app:headerLayout="@layout/information_head_item" app:menu="@menu/setting_menu" android:layout_gravity="left" />
其中app:headerLayout代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:circleimageview="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <de.hdodenhof.circleimageview.CircleImageView android:id="@+id/HeadImage" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/user_bule" android:layout_gravity="center" circleimageview:civ_border_color="@android:color/holo_red_light" circleimageview:civ_border_overlay="false" circleimageview:civ_border_width="2dp" circleimageview:civ_fill_color="@android:color/holo_blue_light" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center" android:layout_margin="@dimen/Small"> <TextView android:id="@+id/HeadUserID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="智能停车账号:" android:singleLine="true" android:textSize="@dimen/text_Medium" /> <TextView android:id="@+id/Information_UserName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登陆账号" android:singleLine="true" android:textSize="@dimen/text_Medium" /> </LinearLayout> </LinearLayout>
app:menu代码如下:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/Personal" android:title="个人信息" android:icon="@drawable/user"/> <item android:id="@+id/Pay" android:title="支付管理" android:icon="@drawable/pay1"/> <item android:id="@+id/Car" android:title="车辆管理" android:icon="@drawable/cars"/> <item android:id="@+id/Collection" android:title="共享车位" android:icon="@drawable/shared"/> <item android:id="@+id/File" android:title="提前预定" android:icon="@drawable/booking"/> <item android:id="@+id/OrderManagement" android:title="订单管理" android:icon="@drawable/order"/> </group> <item android:title="Setting"> <menu > <item android:id="@+id/Setting" android:title="设置" android:icon="@drawable/setting1"/> <item android:id="@+id/Version" android:icon="@drawable/version" android:title="版本信息"/> </menu> </item> </menu>
在侧边栏我写了三个功能:个人信息,提前预定,共享车位,订单管理
一:个人信息
XML布局文件代码如下:
<?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="#E0E0E0" android:orientation="vertical" > <!--标题栏--> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:orientation="horizontal" android:paddingLeft="5dp" android:paddingBottom="20dp" android:paddingTop="10dp" android:background="#fff" > <ImageView android:id="@+id/ToSettingInformationGoBack" android:layout_width="20dp" android:layout_height="30dp" android:layout_marginLeft="5dp" android:layout_marginTop="2dp" android:scaleType="fitCenter" android:src="@drawable/goback1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号管理" android:textSize="20sp" android:textColor="#000" android:layout_marginLeft="110dp" android:layout_marginTop="5dp" /> </LinearLayout> <!--信息栏--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingTop="20dp"> <!--头像--> <LinearLayout android:id="@+id/LayoutIcons" android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" android:paddingLeft="20dp" android:background="#fff"> <TextView android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="头像" android:textSize="15sp" android:layout_marginTop="10dp" android:textColor="#000"/> <ImageView android:id="@+id/SettingIcons" android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/icons" android:scaleType="fitCenter" android:layout_marginTop="5dp" android:layout_marginRight="10dp"/> <ImageView android:layout_width="20dp" android:layout_height="20dp" android:src="@drawable/right" android:scaleType="fitCenter" android:layout_marginTop="10dp" android:layout_marginRight="10dp"/> </LinearLayout> <!--昵称--> <LinearLayout android:id="@+id/LayoutName" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="20dp" android:background="#fff"> <TextView android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="昵称" android:textSize="15sp" android:layout_marginTop="10dp" android:textColor="#000"/> <TextView android:id="@+id/SettingName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="FranzLiszt" android:layout_marginTop="10dp" android:layout_marginRight="10dp" android:textColor="#000" /> <ImageView android:layout_width="20dp" android:layout_height="20dp" android:src="@drawable/right" android:scaleType="fitCenter" android:layout_marginTop="10dp" android:layout_marginRight="10dp"/> </LinearLayout> <!--性别--> <LinearLayout android:id="@+id/LayoutSex" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="20dp" android:background="#fff"> <TextView android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="性别" android:textSize="15sp" android:layout_marginTop="10dp" android:textColor="#000"/> <TextView android:id="@+id/SettingSex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginRight="10dp" android:text="男" android:textColor="#000" android:background="#fff"/> <ImageView android:layout_width="20dp" android:layout_height="20dp" android:src="@drawable/right" android:scaleType="fitCenter" android:layout_marginTop="10dp" android:layout_marginRight="10dp"/> </LinearLayout> <!--等级--> <LinearLayout android:id="@+id/LayoutLevel" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="20dp" android:background="#fff"> <TextView android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="等级" android:textSize="15sp" android:layout_marginTop="10dp" android:textColor="#000"/> <TextView android:id="@+id/SettingLevel" android:layout_width="wrap_content" android:layout_height="30dp" android:text="铜牌会员" android:layout_marginTop="10dp" android:layout_marginRight="10dp" android:textColor="#000" android:background="#fff"/> <ImageView android:layout_width="20dp" android:layout_height="20dp" android:src="@drawable/right" android:scaleType="fitCenter" android:layout_marginTop="10dp" android:layout_marginRight="10dp"/> </LinearLayout> <!--手机号码--> <LinearLayout android:id="@+id/LayoutPhoneNumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="20dp" android:background="#fff"> <TextView android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="手机号码" android:textSize="15sp" android:layout_marginTop="10dp" android:textColor="#000"/> <TextView android:id="@+id/SettingPhoneNumber" android:layout_width="wrap_content" android:layout_height="30dp" android:text="去绑定" android:layout_marginTop="10dp" android:layout_marginRight="10dp" android:textColor="#000" android:background="#fff"/> <ImageView android:layout_width="20dp" android:layout_height="20dp" android:src="@drawable/right" android:scaleType="fitCenter" android:layout_marginTop="10dp" android:layout_marginRight="10dp"/> </LinearLayout> </LinearLayout> <Button android:id="@+id/Exit" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="退出登陆" android:background="#fff" android:textColor="#ff0000" android:layout_marginTop="20dp"/> </LinearLayout>
JAVA代码如下:
public class SettingInformation extends AppCompatActivity { private LinearLayout LayoutImage,LayoutSex; private PopupWindow popupWindow; private TextView SettingName,SettingSex,SettingPhoneNumber,SettingLevel; private View view = null; private View viewSex = null; private Button ToCamera,ToPhoto,ToExit,Exit; private static int REQUEST_CAMERA = 1; private static int REQUEST_CHOOSE_PHOTO = 2; private String PhotoAddress; private ImageView SettingIcons,SettingGoBack; private Bitmap bitmap; private static final String[] sPermissonVal = {Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE}; private static final int PERMISSION_CAMERA_REQUEST_CODE = 3; Dao dao = new Dao( SettingInformation.this ); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_setting_information ); InitView(); ShowPopupWindows(); UpdateLayout(); Listener(); //GetPhoto(); //ShowPopupWindows_Sex(); } /****************初始化界面****************/ private void InitView() { LayoutImage = findViewById( R.id.LayoutIcons ); LayoutSex = findViewById( R.id.LayoutSex ); Exit = findViewById( R.id.Exit ); SettingIcons = findViewById( R.id.SettingIcons ); //CameraActivity.setClipRatio(1, 1); SettingName = findViewById( R.id.SettingName ); SettingPhoneNumber = findViewById( R.id.SettingPhoneNumber ); SettingSex = findViewById( R.id.SettingSex ); SettingGoBack = findViewById( R.id.ToSettingInformationGoBack ); SettingLevel = findViewById( R.id.SettingLevel ); PhotoAddress = Environment.getExternalStorageDirectory().getPath(); //指定路径和获取图片的名称 PhotoAddress = PhotoAddress+"/"+"Image.png"; } /****************根据注册填写的信息改变此布局内容,传过来一个boolean值进行判断****************/ private void UpdateLayout(){ if (Login.JudgeLoginStatic == true) { Account account = dao.QueryInformation( Login.Login_Account ); String GetSex = account.getSex(); String GetPhoneNumber = account.getPhoneNumber(); SettingName.setText( Login.Login_Account ); SettingPhoneNumber.setText( GetPhoneNumber ); SettingSex.setText( GetSex ); } } /****************用于点击退出登陆后,清理布局****************/ private void DeleteLayout(){ SettingIcons.setImageResource( R.drawable.icons ); SettingName.setText( "" ); SettingSex.setText( "" ); SettingPhoneNumber.setText( "" ); SettingLevel.setText( "" ); Exit.setText( "立即登陆" ); Intent intent = new Intent( SettingInformation.this, Login.class ); startActivity( intent ); } //申请权限 /************需要动态申请权限************/ private void requestPermission() { // Android6.0开始, 即当API大于等于 23 时,才动态申请权限 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { this.requestPermissions(sPermissonVal, PERMISSION_CAMERA_REQUEST_CODE); } } /****************初始化PopupWindows****************/ private void ShowPopupWindows() { //获取布局 view = LayoutInflater.from( SettingInformation.this).inflate( R.layout.popup_image,null,false ); ToCamera = view.findViewById( R.id.ToCamera ); //跳转相机 ToPhoto = view.findViewById( R.id.ToPhoto );//跳转相册 ToExit = view.findViewById( R.id.ToExit );//退出 popupWindow = new PopupWindow( view, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT ); popupWindow.setFocusable( true ); //获取焦点 popupWindow.setBackgroundDrawable( new BitmapDrawable( ) ); popupWindow.setOutsideTouchable( true ); //点击外面地方,取消 popupWindow.setTouchable( true ); //允许点击 popupWindow.setAnimationStyle( R.style.MyPopupWindow ); //设置动画 ToExit.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); } } ); /****************打开相机监听事件****************/ ToCamera.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { //requestPermission(); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.addCategory(Intent.CATEGORY_DEFAULT); startActivityForResult(intent, 2); } } ); /****************打开相册监听事件****************/ ToPhoto.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { openGallery(); } } ); } /****************对返回键进行监听,如果按下popupWindow消失****************/ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){ if (popupWindow != null && popupWindow.isShowing()){ popupWindow.dismiss(); return true; } } return false; } /****************显示PopupWindows****************/ private void ShowPopupWindowsFromButtom(){ popupWindow.showAtLocation( view, Gravity.BOTTOM,0,0 ); } /****************打开相机****************/ private void OpenCamera(){ // Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // intent.addCategory(Intent.CATEGORY_DEFAULT); // // // 打开照相机,设置请求码 // intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); // startActivityForResult(intent, 2); // try { // Intent intent = new Intent(); // intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); // File file = FileManager.createFileIfNeed("filepath.png"); // Uri uri; // if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) { // uri = Uri.fromFile(file); // } else { // uri = FileProvider.getUriForFile(this, "link.android.file.provider", file); // } // intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); // intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // startActivityForResult(intent, 1); // }catch (IOException e){ // e.printStackTrace(); // } } /****************打卡相册****************/ private void openGallery() { Intent picture = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(picture, 2); } @Override protected void onResume() { super.onResume(); //获得相册、相机返回的结果,并显示 if (CameraActivity.LISTENING) { Log.e("TAG", "返回的Uri结果:" + CameraActivity.IMG_URI); Log.e("TAG", "返回的File结果:" + CameraActivity.IMG_File.getPath()); CameraActivity.LISTENING = false; //关闭获取结果 SettingIcons.setImageURI(CameraActivity.IMG_URI); //显示图片到控件 } } /********************************/ @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult( requestCode, resultCode, data ); //判断是否返回数据 if (resultCode == RESULT_OK) { //判断返回的类型是否是我们所需 if (requestCode == 1) { String state = Environment.getExternalStorageState(); if (!state.equals( Environment.MEDIA_MOUNTED )) return; Tiny.FileCompressOptions options = new Tiny.FileCompressOptions(); Tiny.getInstance().source( FileManager.readpic() ).asFile().withOptions( options ).compress( new FileWithBitmapCallback() { @Override public void callback(boolean isSuccess, Bitmap bitmap, String outfile, Throwable t) { } } ); } else if (requestCode == 2 && resultCode == Activity.RESULT_OK && null != data) { // try { // Uri selectedImage = data.getData(); // Tiny.FileCompressOptions options = new Tiny.FileCompressOptions(); // Tiny.getInstance().source( selectedImage ).asFile().withOptions( options ).compress( new FileWithBitmapCallback() { // @Override // public void callback(boolean isSuccess, Bitmap bitmap, String outfile, Throwable t) { // SettingIcons.setImageBitmap( bitmap ); // } // } ); // } catch (Exception e) { // e.printStackTrace(); // } Bundle bundle = data.getExtras(); Bitmap bitmap = (Bitmap) bundle.get("data"); SettingIcons.setImageBitmap(bitmap); } } } private void getPicture() { //获取字符串 SharedPreferences sPreferences = getSharedPreferences("Picture", Context.MODE_PRIVATE); String imageBase64 = sPreferences.getString("cameraImage", ""); //把字符串解码成Bitmap对象 byte[] byte64 = Base64.decode(imageBase64, 0); ByteArrayInputStream bais = new ByteArrayInputStream(byte64); Bitmap bitmap = BitmapFactory.decodeStream(bais); //显示图片 SettingIcons.setImageBitmap(bitmap); } private void ToBitmap(){ //把Bitmap转码成字符串 ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress( Bitmap.CompressFormat.PNG, 50,baos); String imageBase64 = new String ( Base64.encode(baos.toByteArray(), 0)); //把字符串存到SharedPreferences里面 SharedPreferences prePicture = getSharedPreferences("Picture", Context.MODE_PRIVATE); SharedPreferences.Editor editor = prePicture.edit(); editor.putString("cameraImage", imageBase64); editor.commit(); } private void Listener () { OnClick onClick = new OnClick(); LayoutImage.setOnClickListener( onClick ); LayoutSex.setOnClickListener( onClick ); Exit.setOnClickListener( onClick ); SettingGoBack.setOnClickListener( onClick ); } /**************内部类监听事件**************/ class OnClick implements View.OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.LayoutIcons: ShowPopupWindowsFromButtom(); break; case R.id.Exit: DeleteLayout(); break; case R.id.ToSettingInformationGoBack: Intent intent = new Intent( SettingInformation.this, Function.class); startActivity( intent ); break; } } } }
二:共享车位
步骤:1.选择共享车位类型2.填写相关信息3.审核
三:提前预定
效果图如下:
从右网左侧滑出现提前预定按钮
订单信息
弹出信息确认提示
四:订单管理
当确认订单时,便开始计时:
界面如下:
然后点击结束并缴费,弹出popwindows,订单信息
点击确定之后进入支付界面,依旧是弹出popwindows,界面如下
填写密码之后,点击支付,弹出通知,回执支付成功信息
介绍几个重要的功能
1.计时器
返回系统启动到现在的毫秒数,包含休眠时间。
SystemClock.elapsedRealtime()
Handler handler_time = new Handler( ){ @Override public void handleMessage(@NonNull Message msg) { super.handleMessage( msg ); mHour.setText( msg.obj.toString() ); } }; new Timer( ).scheduleAtFixedRate( new TimerTask() { @Override public void run() { int time = (int)((SystemClock.elapsedRealtime() - OrderInterface.this.CurrentTime) / 1000); GetTimeHour = new DecimalFormat("00").format(time / 3600); GetTimeMinute = new DecimalFormat("00").format(time % 3600 / 60); GetTimeSecond = new DecimalFormat("00").format(time % 60); String timeFormat = new String(GetTimeHour + ":" + GetTimeMinute + ":" + GetTimeSecond); Message msg = new Message(); msg.obj = timeFormat; handler_time.sendMessage(msg); } },0,1000L ); }
2.通知
Android8以上使用通知一定要创建渠道
private void NotificationManagerMethod(){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //创建通知渠道ID String channelId = "Park"; //创建通知渠道名称 String channelName = "路边秒停车"; //创建通知渠道重要性 int importance = NotificationManager.IMPORTANCE_HIGH; createNotificationChannel( channelId, channelName, importance ); } } private void InitNotificationManager(){ manager = (NotificationManager)getSystemService( NOTIFICATION_SERVICE ); Notification builder = new NotificationCompat.Builder(this,"Park") .setSmallIcon( R.drawable.stopicon_notfition ) .setContentTitle( "支付结果" ) .setContentText( "支付成功\n"+"停车时间:"+GetTimeHour+"小时"+GetTimeMinute+"分钟"+"\n"+"支付金额:"+PayTotalMoney+"元" ) // 通知首次出现在通知栏,带上升动画效果的 .setWhen( System.currentTimeMillis() ) .setAutoCancel(true) .build(); manager.notify( 0,builder); } private void createNotificationChannel(String channelId,String channelName,int importance){ NotificationChannel channel = new NotificationChannel(channelId, channelName, importance); NotificationManager notificationManager = (NotificationManager) getSystemService( NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(channel); }
该类整体代码如下:
package com.example.Order; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.NotificationCompat; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Context; import android.content.Intent; import android.graphics.drawable.BitmapDrawable; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.SystemClock; import android.text.Layout; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; import com.example.Imformation.Function; import com.example.intelligentparking.R; import com.mob.tools.utils.ResHelper; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Random; import java.util.Timer; import java.util.TimerTask; public class OrderInterface extends AppCompatActivity { private TextView mHour,mMinute,mSecond; private ImageView mOrderGoBack; private Button mToPay; private int CurrentMinute = 0,CurrentHour = 0,CurrentSecond = 0; private Handler handler; private Toast toastPayInformation; private PopupWindow DisplayPayInterface,InputPassWordInterface; private long CurrentTime; private String DEFAULT_TIME_FORMAT = "yyyy-MM-dd hh:mm:ss"; public static final int ParkPrice = 5; private String GetTimeHour; private String GetTimeMinute; private String GetTimeSecond; private int PayTotalMoney = 0; /**以下为PopupWindow里面的控件*/ private TextView Name,PhoneNumber,ParkedTime,EstimatePayMoney,ReallyPayMoney,OrderTime,OrderNumber; private Button Cancel,Confirm; private View view,view_passWord; private Thread thread; private NotificationManager manager; /**PopupWindow支付并且填写密码界面*/ private ImageView GoBackPayInterface; private TextView PayMoney; private EditText PassWord_One,PassWord_Two,PassWord_Three,PassWord_Four,PassWord_Five,PassWord_Six; private Button ConfirmPay; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); //requestWindowFeature( Window.FEATURE_CUSTOM_TITLE ); setContentView( R.layout.activity_order_interface ); //getWindow().setFeatureInt( Window.FEATURE_CUSTOM_TITLE,R.layout.order_titlebar ); OrderInterface.this.CurrentTime = SystemClock.elapsedRealtime(); InitView(); Listener(); DisplayPayInformation(); DisplayInputPassWordAndPay(); NotificationManagerMethod(); Handler handler_time = new Handler( ){ @Override public void handleMessage(@NonNull Message msg) { super.handleMessage( msg ); mHour.setText( msg.obj.toString() ); } }; new Timer( ).scheduleAtFixedRate( new TimerTask() { @Override public void run() { /* 返回系统启动到现在的毫秒数,包含休眠时间。*/ int time = (int)((SystemClock.elapsedRealtime() - OrderInterface.this.CurrentTime) / 1000); GetTimeHour = new DecimalFormat("00").format(time / 3600); GetTimeMinute = new DecimalFormat("00").format(time % 3600 / 60); GetTimeSecond = new DecimalFormat("00").format(time % 60); String timeFormat = new String(GetTimeHour + ":" + GetTimeMinute + ":" + GetTimeSecond); Message msg = new Message(); msg.obj = timeFormat; handler_time.sendMessage(msg); } },0,1000L ); } private void InitView(){ mHour = findViewById( R.id.Time_Hour ); /* mMinute = findViewById( R.id.Time_Minute ); mSecond = findViewById( R.id.Time_Second );*/ mOrderGoBack = findViewById( R.id. OrderBack); mToPay = findViewById( R.id.ToPay ); } private void Listener(){ OnClick onClick = new OnClick(); mOrderGoBack.setOnClickListener( onClick ); mToPay.setOnClickListener( onClick); } private void NotificationManagerMethod(){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //创建通知渠道ID String channelId = "Park"; //创建通知渠道名称 String channelName = "路边秒停车"; //创建通知渠道重要性 int importance = NotificationManager.IMPORTANCE_HIGH; createNotificationChannel( channelId, channelName, importance ); } } private void InitNotificationManager(){ manager = (NotificationManager)getSystemService( NOTIFICATION_SERVICE ); Notification builder = new NotificationCompat.Builder(this,"Park") .setSmallIcon( R.drawable.stopicon_notfition ) .setContentTitle( "支付结果" ) .setContentText( "支付成功\n"+"停车时间:"+GetTimeHour+"小时"+GetTimeMinute+"分钟"+"\n"+"支付金额:"+PayTotalMoney+"元" ) // 通知首次出现在通知栏,带上升动画效果的 .setWhen( System.currentTimeMillis() ) .setAutoCancel(true) .build(); manager.notify( 0,builder); } private void createNotificationChannel(String channelId,String channelName,int importance){ NotificationChannel channel = new NotificationChannel(channelId, channelName, importance); NotificationManager notificationManager = (NotificationManager) getSystemService( NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(channel); } private void DisplayInputPassWordAndPay(){ view_passWord = LayoutInflater.from( OrderInterface.this ).inflate( R.layout.paysystem_interface,null,false ); GoBackPayInterface = view_passWord.findViewById( R.id.GoBackPayInterface ); PayMoney = view_passWord.findViewById( R.id.PayMoney ); PassWord_One = view_passWord.findViewById( R.id.PassWord_one ); PassWord_Two = view_passWord.findViewById( R.id.PassWord_two ); PassWord_Three = view_passWord.findViewById( R.id.PassWord_three ); PassWord_Four = view_passWord.findViewById( R.id.PassWord_four ); PassWord_Five = view_passWord.findViewById( R.id.PassWord_five ); PassWord_Six = view_passWord.findViewById( R.id.PassWord_six ); ConfirmPay = view_passWord.findViewById( R.id.ConfirmPay ); InputPassWordInterface = new PopupWindow( view_passWord,ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT ); InputPassWordInterface.setFocusable( true ); //获取焦点 InputPassWordInterface.setBackgroundDrawable( new BitmapDrawable( ) ); InputPassWordInterface.setOutsideTouchable( true ); //点击外面地方,取消 InputPassWordInterface.setTouchable( true ); //允许点击 InputPassWordInterface.setAnimationStyle( R.style.MyPopupWindow ); //设置动画 ConfirmPay.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { InputPassWordInterface.dismiss(); InitNotificationManager(); } } ); GoBackPayInterface.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { InputPassWordInterface.dismiss(); } } ); } private void DisplayPayInformation(){ view = LayoutInflater.from( OrderInterface.this ).inflate( R.layout.display_pay_information,null,false ); Name = view.findViewById( R.id.DisplayPay_Name ); PhoneNumber = view.findViewById( R.id.DisplayPay_PhoneNumber ); ParkedTime = view.findViewById( R.id.DisplayPay_Time ); EstimatePayMoney = view.findViewById( R.id.DisplayPay_EstimatePayMoney ); ReallyPayMoney = view.findViewById( R.id.DisplayPay_ReallyPayMoney ); OrderTime = view.findViewById( R.id.OrderTime ); OrderNumber = view.findViewById( R.id.OrderNumber ); Cancel = view.findViewById( R.id.CancelToPay ); Confirm = view.findViewById( R.id.ConfirmToPay ); DisplayPayInterface = new PopupWindow( view, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT ); DisplayPayInterface.setFocusable( true ); //获取焦点 DisplayPayInterface.setBackgroundDrawable( new BitmapDrawable( ) ); DisplayPayInterface.setOutsideTouchable( true ); //点击外面地方,取消 DisplayPayInterface.setTouchable( true ); //允许点击 DisplayPayInterface.setAnimationStyle( R.style.MyPopupWindow ); //设置动画 Cancel.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { DisplayPayInterface.dismiss(); } } ); Confirm.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { DisplayPayInterface.dismiss(); DisplayPayPopWindowsForCenter(); } } ); } private void DisplayPayPopWindowsForCenter(){ InputPassWordInterface.showAtLocation( view,Gravity.BOTTOM,100,50 ); } private void DisplayPopWindowsForCenter(){ Name.setText( "孙鹏" ); PhoneNumber.setText( "17375527572" ); int Hour = Integer.parseInt( GetTimeHour ); int Minute = Integer.parseInt( GetTimeMinute ); int Second = Integer.parseInt( GetTimeSecond ); ParkedTime.setText( Hour+"小时"+Minute+"分钟"+Second+"秒" ); /** * 停车收费标准如下: * 1.一小时单价5元 * 2.大于半小时,不满一小时,归于一小时计 * 3.小于半小时,不计收费*/ if (Hour > 0 && Minute > 30){ PayTotalMoney = (Hour+1)*ParkPrice; }else if (Hour > 0 && Minute < 30){ PayTotalMoney = Hour*ParkPrice; }else { PayTotalMoney = 0; } EstimatePayMoney.setText( PayTotalMoney+"元" ); ReallyPayMoney.setText( PayTotalMoney+"元" ); SimpleDateFormat format = new SimpleDateFormat( DEFAULT_TIME_FORMAT ); String Time = format.format( Calendar.getInstance().getTime() ); OrderTime.setText( Time ); Random random = new Random( ); Long OrderNumber_Long = 0L; do { OrderNumber_Long = random.nextLong(); }while (OrderNumber_Long < 0); OrderNumber.setText( OrderNumber_Long+"" ); DisplayPayInterface.showAtLocation( view,Gravity.CENTER,0,0 ); } class OnClick implements View.OnClickListener{ @Override public void onClick(View v) { switch (v.getId()){ case R.id.OrderBack: startActivity( new Intent( OrderInterface.this, Function.class ) ); break; case R.id.ToPay: DisplayPopWindowsForCenter(); break; } } } }
总结
实践出真知