Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
MiYaFastCashier
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
jiangjiantao
MiYaFastCashier
Commits
940fe10e
Commit
940fe10e
authored
Mar 03, 2023
by
赵鹏翔
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
核心业务类:扫码类处理
parent
497b78a2
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
115 additions
and
137 deletions
+115
-137
PayActivity.kt
app/src/main/java/com/miya/fastcashier/ui/PayActivity.kt
+12
-5
ScanGunKeyEventHelper.java
...java/com/miya/fastcashier/util/ScanGunKeyEventHelper.java
+0
-132
ScanGunKeyEventHelper.kt
...n/java/com/miya/fastcashier/util/ScanGunKeyEventHelper.kt
+103
-0
No files found.
app/src/main/java/com/miya/fastcashier/ui/PayActivity.kt
View file @
940fe10e
...
...
@@ -53,14 +53,21 @@ class PayActivity : BaseActivity() {
})
binding
.
clTitle
.
llBack
.
clickWithTrigger
{
finish
()
}
scanGunKeyEventHelper
.
setOnBarCodeCatchListener
{
showProgressDialog
(
"正在支付.."
)
viewModel
.
pay
(
price
,
it
)
}
scanGunKeyEventHelper
.
setOnBarCodeCatchListener
(
object
:
ScanGunKeyEventHelper
.
OnScanSuccessListener
{
override
fun
onScanSuccess
(
barcode
:
String
?)
{
barcode
?.
let
{
showProgressDialog
(
"正在支付.."
)
viewModel
.
pay
(
price
,
it
)
}
}
})
}
override
fun
dispatchKeyEvent
(
event
:
KeyEvent
?):
Boolean
{
scanGunKeyEventHelper
.
analysisKeyEvent
(
event
)
if
(
event
!=
null
)
{
scanGunKeyEventHelper
.
analysisKeyEvent
(
event
)
}
return
super
.
dispatchKeyEvent
(
event
)
}
...
...
app/src/main/java/com/miya/fastcashier/util/ScanGunKeyEventHelper.java
deleted
100644 → 0
View file @
497b78a2
package
com
.
miya
.
fastcashier
.
util
;
import
android.os.Handler
;
import
android.text.TextUtils
;
import
android.view.KeyEvent
;
/**
* 扫码枪解码器
* Created by fengyu on 2018/1/12.
*/
public
class
ScanGunKeyEventHelper
{
//延迟500ms,判断扫码是否完成。
private
final
static
long
MESSAGE_DELAY
=
500
;
//扫码内容
private
final
StringBuffer
mStringBufferResult
=
new
StringBuffer
();
//大小写区分
private
boolean
mCaps
;
private
OnScanSuccessListener
mOnScanSuccessListener
;
private
final
Handler
mHandler
=
new
Handler
();
private
final
Runnable
mScanningFishedRunnable
=
new
Runnable
()
{
@Override
public
void
run
()
{
performScanSuccess
();
}
};
//返回扫描结果
private
void
performScanSuccess
()
{
String
barcode
=
mStringBufferResult
.
toString
();
if
(
mOnScanSuccessListener
!=
null
&&
!
TextUtils
.
isEmpty
(
barcode
))
mOnScanSuccessListener
.
onScanSuccess
(
barcode
);
mStringBufferResult
.
setLength
(
0
);
}
//key事件处理
public
void
analysisKeyEvent
(
KeyEvent
event
)
{
int
keyCode
=
event
.
getKeyCode
();
//字母大小写判断
checkLetterStatus
(
event
);
if
(
event
.
getAction
()
==
KeyEvent
.
ACTION_DOWN
)
{
char
aChar
=
getInputCode
(
event
);
if
(
keyCode
==
KeyEvent
.
KEYCODE_ENTER
)
{
//若为回车键,直接返回
mHandler
.
removeCallbacks
(
mScanningFishedRunnable
);
mHandler
.
post
(
mScanningFishedRunnable
);
}
else
{
if
(
aChar
!=
0
)
{
mStringBufferResult
.
append
(
aChar
);
}
//延迟post,若500ms内,有其他事件
mHandler
.
removeCallbacks
(
mScanningFishedRunnable
);
mHandler
.
postDelayed
(
mScanningFishedRunnable
,
MESSAGE_DELAY
);
}
}
}
//检查shift键
private
void
checkLetterStatus
(
KeyEvent
event
)
{
int
keyCode
=
event
.
getKeyCode
();
if
(
keyCode
==
KeyEvent
.
KEYCODE_SHIFT_RIGHT
||
keyCode
==
KeyEvent
.
KEYCODE_SHIFT_LEFT
)
{
if
(
event
.
getAction
()
==
KeyEvent
.
ACTION_DOWN
)
{
//按着shift键,表示大写
mCaps
=
true
;
}
else
{
//松开shift键,表示小写
mCaps
=
false
;
}
}
}
//获取扫描内容
private
char
getInputCode
(
KeyEvent
event
)
{
int
keyCode
=
event
.
getKeyCode
();
char
aChar
;
if
(
keyCode
>=
KeyEvent
.
KEYCODE_A
&&
keyCode
<=
KeyEvent
.
KEYCODE_Z
)
{
//字母
aChar
=
(
char
)
((
mCaps
?
'A'
:
'a'
)
+
keyCode
-
KeyEvent
.
KEYCODE_A
);
}
else
if
(
keyCode
>=
KeyEvent
.
KEYCODE_0
&&
keyCode
<=
KeyEvent
.
KEYCODE_9
)
{
//数字
aChar
=
(
char
)
(
'0'
+
keyCode
-
KeyEvent
.
KEYCODE_0
);
}
else
{
//其他符号
switch
(
keyCode
)
{
case
KeyEvent
.
KEYCODE_PERIOD
:
aChar
=
'.'
;
break
;
case
KeyEvent
.
KEYCODE_MINUS
:
aChar
=
mCaps
?
'_'
:
'-'
;
break
;
case
KeyEvent
.
KEYCODE_SLASH
:
aChar
=
'/'
;
break
;
case
KeyEvent
.
KEYCODE_BACKSLASH
:
aChar
=
mCaps
?
'|'
:
'\\'
;
break
;
default
:
aChar
=
0
;
break
;
}
}
return
aChar
;
}
public
interface
OnScanSuccessListener
{
public
void
onScanSuccess
(
String
barcode
);
}
public
void
setOnBarCodeCatchListener
(
OnScanSuccessListener
onScanSuccessListener
)
{
mOnScanSuccessListener
=
onScanSuccessListener
;
}
public
void
onDestroy
()
{
mHandler
.
removeCallbacks
(
mScanningFishedRunnable
);
mOnScanSuccessListener
=
null
;
}
}
app/src/main/java/com/miya/fastcashier/util/ScanGunKeyEventHelper.kt
0 → 100644
View file @
940fe10e
package
com.miya.fastcashier.util
import
android.os.Handler
import
android.text.TextUtils
import
android.view.KeyEvent
/**
* 扫码枪解码器
* Created by fengyu on 2018/1/12.
*/
class
ScanGunKeyEventHelper
{
//扫码内容
private
val
mStringBufferResult
=
StringBuffer
()
//大小写区分
private
var
mCaps
=
false
private
var
mOnScanSuccessListener
:
OnScanSuccessListener
?
=
null
private
val
mHandler
=
Handler
()
private
val
mScanningFishedRunnable
=
Runnable
{
performScanSuccess
()
}
//返回扫描结果
private
fun
performScanSuccess
()
{
val
barcode
=
mStringBufferResult
.
toString
()
if
(
mOnScanSuccessListener
!=
null
&&
!
TextUtils
.
isEmpty
(
barcode
))
mOnScanSuccessListener
!!
.
onScanSuccess
(
barcode
)
mStringBufferResult
.
setLength
(
0
)
}
//key事件处理
fun
analysisKeyEvent
(
event
:
KeyEvent
)
{
val
keyCode
=
event
.
keyCode
//字母大小写判断
checkLetterStatus
(
event
)
if
(
event
.
action
==
KeyEvent
.
ACTION_DOWN
)
{
val
aChar
=
getInputCode
(
event
)
if
(
keyCode
==
KeyEvent
.
KEYCODE_ENTER
)
{
//若为回车键,直接返回
mHandler
.
removeCallbacks
(
mScanningFishedRunnable
)
mHandler
.
post
(
mScanningFishedRunnable
)
}
else
{
if
(
aChar
.
toInt
()
!=
0
)
{
mStringBufferResult
.
append
(
aChar
)
}
//延迟post,若500ms内,有其他事件
mHandler
.
removeCallbacks
(
mScanningFishedRunnable
)
mHandler
.
postDelayed
(
mScanningFishedRunnable
,
MESSAGE_DELAY
)
}
}
}
//检查shift键
private
fun
checkLetterStatus
(
event
:
KeyEvent
)
{
val
keyCode
=
event
.
keyCode
if
(
keyCode
==
KeyEvent
.
KEYCODE_SHIFT_RIGHT
||
keyCode
==
KeyEvent
.
KEYCODE_SHIFT_LEFT
)
{
//true为按下shift键,表示大写
mCaps
=
(
event
.
action
==
KeyEvent
.
ACTION_DOWN
)
}
}
//获取扫描内容
private
fun
getInputCode
(
event
:
KeyEvent
):
Char
{
val
keyCode
=
event
.
keyCode
val
aChar
:
Char
aChar
=
if
(
keyCode
>=
KeyEvent
.
KEYCODE_A
&&
keyCode
<=
KeyEvent
.
KEYCODE_Z
)
{
//字母
((
if
(
mCaps
)
'A'
else
'a'
).
toInt
()
+
keyCode
-
KeyEvent
.
KEYCODE_A
).
toChar
()
}
else
if
(
keyCode
>=
KeyEvent
.
KEYCODE_0
&&
keyCode
<=
KeyEvent
.
KEYCODE_9
)
{
//数字
(
'0'
.
toInt
()
+
keyCode
-
KeyEvent
.
KEYCODE_0
).
toChar
()
}
else
{
//其他符号
when
(
keyCode
)
{
KeyEvent
.
KEYCODE_PERIOD
->
'.'
KeyEvent
.
KEYCODE_MINUS
->
if
(
mCaps
)
'_'
else
'-'
KeyEvent
.
KEYCODE_SLASH
->
'/'
KeyEvent
.
KEYCODE_BACKSLASH
->
if
(
mCaps
)
'|'
else
'\\'
else
->
0
.
toChar
()
}
}
return
aChar
}
interface
OnScanSuccessListener
{
fun
onScanSuccess
(
barcode
:
String
?)
}
fun
setOnBarCodeCatchListener
(
onScanSuccessListener
:
OnScanSuccessListener
?)
{
mOnScanSuccessListener
=
onScanSuccessListener
}
fun
onDestroy
()
{
mHandler
.
removeCallbacks
(
mScanningFishedRunnable
)
mOnScanSuccessListener
=
null
}
companion
object
{
//延迟500ms,判断扫码是否完成。
private
const
val
MESSAGE_DELAY
:
Long
=
500
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment