Commit 6e4ceca7 authored by gaoyingxiang's avatar gaoyingxiang

自定义数字键盘

parent 767e1888
package com.qmai.android.keyboard;
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.inputmethodservice.Keyboard
import android.inputmethodservice.KeyboardView
import android.util.AttributeSet
import android.util.Log
import android.util.TypedValue
import android.widget.EditText
import com.qmai.android.keyboardview.R
class CommonKeyBoardKeyBoard : KeyboardView {
private lateinit var keys: List<Keyboard.Key>
private var editWidget: EditText? = null
private lateinit var mtextPaint: Paint
//默认值
private var mSureDefaultColor = Color.parseColor("#ffffff")//文字的默认颜色
private var mClearDefaultColor = Color.parseColor("#000000")//文字的默认颜色
//最大的按钮 “确定”
var btnText: String? = null
var sureTextColor:Int? = null
var sureTextSize:Float? = null
//右上角的按钮 “清空”
var rightTopBtn: String? = null
var clearTextColor:Int? = null
var clearTextSize:Float? = null
//是否可以使用点
var pointColor:Int? = null
var pointSize:Float? = null
//获取drawable资源
val drawable = context.resources.getDrawable(R.drawable.savestock_blue)
private var defaultActionListener: DefaultKeyBoardActionListener? =
DefaultKeyBoardActionListener()
enum class Code constructor(var code: Int) {
DELETE(10), CLEAR(11), SAVE(-11)
}
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
init(context)
}
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
init(context)
attrs?.let { retrieveAttributes(attrs) }
}
fun bindEditText(editText: EditText?) {
editWidget = editText
defaultActionListener?.et = editText
}
fun setKeyBoardLister(keyBoardListener: DefaultKeyBoardActionListener?) {
defaultActionListener = keyBoardListener
onKeyboardActionListener = defaultActionListener
}
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
if (!::keys.isInitialized) {
keys = getKeyboard().keys
}
canvas?.let { c ->
val keys: List<Keyboard.Key> = keyboard.keys
keys.forEach { key ->
if (key.codes[0] == -11) {
//把真正的状态设置给上层绘制的key键
drawable.state = key.currentDrawableState
//rawable将在被绘制在canvas的哪个矩形区域内(这个矩形区域的坐标是以canvas左上角为坐标原点的)
drawable.setBounds(
key.x,
key.y + paddingTop,
key.x + key.width + 2,
key.y + key.height + 4
)
//开始绘制
drawable.draw(canvas)
if (key.label != null) {
val fontMetrics = mtextPaint.fontMetrics
mtextPaint.color = sureTextColor?:mSureDefaultColor
mtextPaint.textSize = sp2px(sureTextSize?:15f)
//计算基线
val baseLine =
key.y + (key.height) / 2 - (fontMetrics.bottom - fontMetrics.top) / 2
//绘制文字
canvas.drawText(
btnText?:"确定",
(key.x + ((key.width) / 2)).toFloat(), baseLine, mtextPaint
)
}
} else if (key.codes[0] == 11) {
//开始绘制
if (key.label != null) {
val fontMetrics = mtextPaint.fontMetrics
mtextPaint.color = clearTextColor?:mClearDefaultColor
mtextPaint.textSize = sp2px(clearTextSize?:15f)
mtextPaint.textAlign = Paint.Align.CENTER
mtextPaint.color = Color.parseColor("#333333")
//计算基线
val baseLine =
key.y + (key.height) / 2 - (fontMetrics.bottom - fontMetrics.top) / 2
//绘制文字
canvas.drawText(
rightTopBtn?:"取消",
(key.x + ((key.width) / 2)).toFloat(),
baseLine + key.height / 2,
mtextPaint
)
}
}else if (key.codes[0]==20){
//开始绘制
if (key.label != null) {
val fontMetrics = mtextPaint.fontMetrics
if (pointColor!=null&&pointColor!=0){
mtextPaint.color = pointColor?:mClearDefaultColor
}
mtextPaint.textSize = sp2px(pointSize?:15f)
mtextPaint.textAlign = Paint.Align.CENTER
mtextPaint.color = Color.parseColor("#333333")
//计算基线
val baseLine =
key.y + (key.height) / 2 - (fontMetrics.bottom - fontMetrics.top) / 2
//绘制文字
canvas.drawText(
".",
(key.x + ((key.width) / 2)).toFloat(),
baseLine + key.height / 3,
mtextPaint
)
}
}
}
}
}
open class DefaultKeyBoardActionListener : OnKeyboardActionListener {
var et: EditText? = null
override fun swipeRight() {
}
override fun onPress(primaryCode: Int) {
}
override fun onRelease(primaryCode: Int) {
}
override fun swipeLeft() {
}
override fun swipeUp() {
}
override fun swipeDown() {
}
override fun onKey(primaryCode: Int, keyCodes: IntArray?) {
et ?: return
val editable = et!!.editableText
when (primaryCode) {
Code.CLEAR.code -> {
editable.clear()
}
Code.DELETE.code -> {
if (editable.isNotEmpty()) {
editable.delete(editable.length - 1, editable.length)
}
}
20 -> {
editable.append(".")
}
21 -> {
editable.append("00")
}
Code.SAVE.code -> {
}
else -> {
editable.append(primaryCode.toString())
}
}
}
override fun onText(text: CharSequence?) {
}
}
private fun init(context: Context?) {
val keyBoard = Keyboard(context, R.xml.inputphonekeyboard)
isEnabled = true
isPreviewEnabled = false
this.keyboard = keyBoard
onKeyboardActionListener = defaultActionListener
mtextPaint = Paint(Paint.ANTI_ALIAS_FLAG)
mtextPaint.textAlign = Paint.Align.CENTER
mtextPaint.textSize = sp2px(20f)
mtextPaint.color = Color.parseColor("#333333")
keys = getKeyboard().keys
}
private fun retrieveAttributes(attrs: AttributeSet) {
val typedArray =
context.obtainStyledAttributes(attrs, R.styleable.CommonKeyBoardKeyBoardStyleable)
for (i in 0 until typedArray.indexCount) {
when (typedArray.getIndex(i)) {
R.styleable.CommonKeyBoardKeyBoardStyleable_sureText -> {
btnText =
typedArray.getString(R.styleable.CommonKeyBoardKeyBoardStyleable_sureText)
}
R.styleable.CommonKeyBoardKeyBoardStyleable_clearText -> {
rightTopBtn = typedArray.getString(R.styleable.CommonKeyBoardKeyBoardStyleable_clearText)
}
R.styleable.CommonKeyBoardKeyBoardStyleable_sureTextColor -> {
sureTextColor = typedArray.getColor(
R.styleable.CommonKeyBoardKeyBoardStyleable_sureTextColor,
mSureDefaultColor
)
}
R.styleable.CommonKeyBoardKeyBoardStyleable_clearTextColor -> {
clearTextColor = typedArray.getColor(
R.styleable.CommonKeyBoardKeyBoardStyleable_clearTextColor,
mClearDefaultColor
)
}
R.styleable.CommonKeyBoardKeyBoardStyleable_sureTextSize -> {
sureTextSize = typedArray.getDimension(
R.styleable.CommonKeyBoardKeyBoardStyleable_sureTextSize,15f)
}
R.styleable.CommonKeyBoardKeyBoardStyleable_clearTextSize -> {
clearTextSize = typedArray.getDimension(
R.styleable.CommonKeyBoardKeyBoardStyleable_clearTextSize,15f)
}
R.styleable.CommonKeyBoardKeyBoardStyleable_pointColor->{
pointColor = typedArray.getColor(R.styleable.CommonKeyBoardKeyBoardStyleable_pointColor,0)
}
R.styleable.CommonKeyBoardKeyBoardStyleable_pointSize->{
pointSize = typedArray.getDimension(R.styleable.CommonKeyBoardKeyBoardStyleable_pointSize,15f)
}
}
}
//销毁
typedArray.recycle()
}
private fun sp2px(spValue: Float): Float {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP,
spValue,
resources.displayMetrics
)
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
Log.d("TAG", "onDetachedFromWindow: ")
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#3478F3" />
</shape>
\ No newline at end of file
...@@ -100,4 +100,16 @@ ...@@ -100,4 +100,16 @@
<attr name="keyboardMode" /> <attr name="keyboardMode" />
</declare-styleable> </declare-styleable>
<!--按钮控件(CommonKeyBoardKeyBoardStyleable)的设置-->
<declare-styleable name="CommonKeyBoardKeyBoardStyleable">
<attr name="sureText" format="string" />
<attr name="sureTextColor" format="reference|color"/>
<attr name="sureTextSize" format="dimension"/>
<attr name="clearTextColor" format="reference|color"/>
<attr name="clearText" format="string"/>
<attr name="clearTextSize" format="dimension"/>
<attr name="pointColor" format="reference|color"/>
<attr name="pointSize" format="dimension"/>
</declare-styleable>
</resources> </resources>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyHeight="41dp"
android:horizontalGap="1dp"
android:verticalGap="1dp"
android:keyWidth="25%p">
<Row>
<Key
android:codes="1"
android:keyEdgeFlags="left"
android:keyLabel="1" />
<Key
android:codes="2"
android:keyLabel="2" />
<Key
android:codes="3"
android:keyEdgeFlags="right"
android:keyLabel="3" />
<Key
android:codes="11"
android:keyEdgeFlags="right"
android:isRepeatable="true"
android:keyLabel=""/>
</Row>
<Row>
<Key
android:codes="4"
android:keyEdgeFlags="left"
android:keyLabel="4" />
<Key
android:codes="5"
android:keyLabel="5" />
<Key
android:codes="6"
android:keyLabel="6" />
<Key
android:codes="-11"
android:keyEdgeFlags="right"
android:keyHeight="181dp"
android:keyLabel="结账" />
</Row>
<Row>
<Key
android:codes="7"
android:keyEdgeFlags="left"
android:keyLabel="7" />
<Key
android:codes="8"
android:keyLabel="8" />
<Key
android:codes="9"
android:keyLabel="9" />
<Key
android:codes="-"
android:keyEdgeFlags="right"
android:keyHeight="0dp"
/>
</Row>
<Row>
<Key
android:codes="20"
android:keyEdgeFlags="left"
android:keyLabel="" />
<Key
android:codes="0"
android:keyLabel="0" />
<Key
android:codes="10"
android:isRepeatable="true"
android:keyIcon = "@drawable/icon_key_board_delete"
/>
<Key
android:codes="-"
android:keyEdgeFlags="right"
android:keyHeight="0dp"
/>
</Row>
</Keyboard>
...@@ -10,6 +10,7 @@ import android.os.Environment ...@@ -10,6 +10,7 @@ import android.os.Environment
import android.util.Log import android.util.Log
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.eclipsesource.v8.JavaVoidCallback import com.eclipsesource.v8.JavaVoidCallback
import com.eclipsesource.v8.V8 import com.eclipsesource.v8.V8
import com.eclipsesource.v8.V8Array import com.eclipsesource.v8.V8Array
......
...@@ -112,6 +112,47 @@ ...@@ -112,6 +112,47 @@
android:keyBackground="@drawable/stockbtn_keyboard_key" android:keyBackground="@drawable/stockbtn_keyboard_key"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
<com.qmai.android.keyboard.CommonKeyBoardKeyBoard
android:id="@+id/keyPhone"
android:layout_marginLeft="140dp"
android:layout_marginRight="140dp"
android:layout_marginTop="20dp"
android:background="@drawable/shape_key"
android:paddingRight="1dp"
android:keyTextColor="#333333"
android:paddingTop="1dp"
android:paddingBottom="1dp"
android:paddingLeft="1dp"
android:keyTextSize="16sp"
android:verticalGap= "10dp"
android:shadowColor="#ffffff"
app:pointSize = "120dp"
android:keyBackground="@drawable/stockbtn_keyboard_key"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.qmai.android.keyboard.MoneyNumberKeyBoardKeyBoard
android:id="@+id/moneyKeyboard"
app:layout_constraintTop_toBottomOf="@+id/quickInputRecycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"
android:background="@drawable/shape_key"
android:keyBackground="@drawable/stockbtn_keyboard_key"
android:keyTextColor="@color/black"
android:keyTextSize="18sp"
android:paddingStart="1dp"
android:layout_alignParentBottom="true"
android:paddingTop="1dp"
android:paddingEnd="4dp"
android:paddingBottom="1dp"
android:shadowColor="#ffffff"
android:layout_marginBottom="16dp"
android:layout_gravity="bottom|center_horizontal"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</LinearLayout> </LinearLayout>
......
package com.zhimai.websocket.bean
import org.litepal.crud.LitePalSupport
/**
* @author: 高英祥
* @github:
* @time: 2021/9/29 16:27
* @desc:
* @doc:
*/
class SocketRecordBean : LitePalSupport() {
var time: String? = null
//当时的动作
var action: String? = null
//错误信息
var error: String? = null
//说明
var desc: String? = null
//预留扩展字段1
var otherOne:String? = null
//预留扩展字段2
var otherTwo:String? =null
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment