1 大帅哥隋昊航 2小时前 37次点击
老样子,词代码由梦月大神提供。
require "import"
import "android.app.*"
import "android.os.*"
import "android.widget.*"
import "android.view.*"
import "android.graphics.Color"
import "android.graphics.Rect"
import "android.speech.tts.TextToSpeech"
import "java.util.Locale"
import "java.lang.String"
local tts
local isTtsReady = false
tts = TextToSpeech(activity, TextToSpeech.OnInitListener{
onInit = function(status)
if status == TextToSpeech.SUCCESS then
tts.setLanguage(Locale.CHINESE)
isTtsReady = true
tts.speak("触控键盘已准备就绪。触摸下半屏输入数字,触摸上半屏朗读已输入内容。", TextToSpeech.QUEUE_FLUSH, nil, nil)
else
print("TTS初始化失败")
end
end
})
function speak(text)
if isTtsReady and tts ~= nil then
tts.speak(text, TextToSpeech.QUEUE_FLUSH, nil, nil)
end
end
local inputMode = "number"
local isUpperCase = false
local numberKeyMap = {
{"1", "2", "3"},
{"4", "5", "6"},
{"7", "8", "9"},
{"中英切换", "0", "空格", "删除", "确定"}
}
local englishLowerKeyMap = {
{"q", "w", "e", "r", "t", "y", "u", "i", "o", "p"},
{"a", "s", "d", "f", "g", "h", "j", "k", "l"},
{"大写", "z", "x", "c", "v", "b", "n", "m", "删除"},
{"数字模式", "空格", "确定"}
}
local englishUpperKeyMap = {
{"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"},
{"A", "S", "D", "F", "G", "H", "J", "K", "L"},
{"小写", "Z", "X", "C", "V", "B", "N", "M", "删除"},
{"数字模式", "空格", "确定"}
}
local layout = {
LinearLayout;
id="rootView";
layout_width="fill";
layout_height="fill";
orientation="vertical";
backgroundColor="#EEEEEE";
{
TextView;
id="displayView";
layout_width="fill";
layout_height="0dp";
layout_weight=1;
textSize="40sp";
gravity="center";
text="";
textColor="#212121";
backgroundColor="#FFFFFF";
};
{
LinearLayout;
id="keyboardContainer";
layout_width="fill";
layout_height="0dp";
layout_weight=2;
orientation="vertical";
};
}
activity.setContentView(loadlayout(layout))
local buttonList = {}
function refreshKeyboard()
keyboardContainer.removeAllViews()
buttonList = {}
local currentMap
if inputMode == "number" then
currentMap = numberKeyMap
else
currentMap = isUpperCase and englishUpperKeyMap or englishLowerKeyMap
end
for rowIndex, rowKeys in ipairs(currentMap) do
local rowLayout = LinearLayout(activity)
rowLayout.setOrientation(LinearLayout.HORIZONTAL)
local rowParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
0, 1.0
)
rowLayout.setLayoutParams(rowParams)
for colIndex, keyText in ipairs(rowKeys) do
local btn = TextView(activity)
btn.Text = keyText
btn.TextSize = 28
btn.setGravity(Gravity.CENTER)
btn.setBackgroundColor(0xFFE0E0E0)
btn.setTextColor(0xFF000000)
local weight = 1.0
if rowIndex == 4 and (inputMode == "english" or isUpperCase) then
if colIndex == 2 then
weight = 3.0
end
end
local btnParams = LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.MATCH_PARENT, weight
)
btnParams.setMargins(1, 1, 1, 1)
btn.setLayoutParams(btnParams)
rowLayout.addView(btn)
table.insert(buttonList, btn)
end
keyboardContainer.addView(rowLayout)
end
clearHighlight()
end
function isViewUnder(view, rawX, rawY)
local rect = Rect()
view.getGlobalVisibleRect(rect)
return rect.contains(rawX, rawY)
end
local lastHoveredBtn = nil
local isHoveringDisplay = false
function clearHighlight()
for _, btn in ipairs(buttonList) do
if btn.Text == "确定" then
btn.setBackgroundColor(0xFFBBDEFB)
elseif btn.Text == "删除" then
btn.setBackgroundColor(0xFFFFCDD2)
elseif btn.Text == "中英切换" or btn.Text == "数字模式" then
btn.setBackgroundColor(0xFF2196F3)
elseif btn.Text == "大写" or btn.Text == "小写" then
btn.setBackgroundColor(0xFF4CAF50)
elseif btn.Text == "空格" then
btn.setBackgroundColor(0xFFFFC107)
else
btn.setBackgroundColor(0xFFE0E0E0)
end
end
end
function highlightButton(btn)
clearHighlight()
btn.setBackgroundColor(0xFF4CAF50)
end
refreshKeyboard()
rootView.setOnTouchListener(View.OnTouchListener{
onTouch = function(v, event)
local action = event.getAction()
local x = math.floor(event.getRawX())
local y = math.floor(event.getRawY())
if action == MotionEvent.ACTION_DOWN or action == MotionEvent.ACTION_MOVE then
if isViewUnder(displayView, x, y) then
if not isHoveringDisplay then
isHoveringDisplay = true
if lastHoveredBtn ~= nil then
clearHighlight()
lastHoveredBtn = nil
end
local currentStr = String(displayView.Text).toString()
if currentStr == "" then
speak("当前输入为空")
else
local speakText = currentStr:gsub("%d", "%1 "):gsub("%a", "%1 ")
speak("已输入: " .. speakText)
end
end
else
isHoveringDisplay = false
local currentBtn = nil
for _, btn in ipairs(buttonList) do
if isViewUnder(btn, x, y) then
currentBtn = btn
break
end
end
if currentBtn then
if currentBtn ~= lastHoveredBtn then
lastHoveredBtn = currentBtn
highlightButton(currentBtn)
speak(currentBtn.Text)
end
else
if lastHoveredBtn ~= nil then
clearHighlight()
lastHoveredBtn = nil
end
end
end
elseif action == MotionEvent.ACTION_UP then
if lastHoveredBtn then
local actionText = lastHoveredBtn.Text
local currentStr = String(displayView.Text)
if actionText == "中英切换" then
inputMode = "english"
isUpperCase = false
refreshKeyboard()
speak("切换到英文输入模式,小写")
elseif actionText == "数字模式" then
inputMode = "number"
refreshKeyboard()
speak("切换到数字输入模式")
elseif actionText == "大写" then
isUpperCase = true
refreshKeyboard()
speak("切换到大写")
elseif actionText == "小写" then
isUpperCase = false
refreshKeyboard()
speak("切换到小写")
elseif actionText == "删除" then
if currentStr.length() > 0 then
displayView.Text = currentStr.substring(0, currentStr.length() - 1)
speak("已删除")
else
speak("没有内容可删除")
end
elseif actionText == "确定" then
if currentStr.length() > 0 then
local speakText = currentStr.toString():gsub("%d", "%1 "):gsub("%a", "%1 ")
speak("确认输入:" .. speakText)
else
speak("内容为空")
end
elseif actionText == "空格" then
displayView.Text = displayView.Text .. " "
speak("输入空格")
else
displayView.Text = displayView.Text .. actionText
speak("输入 " .. actionText)
end
clearHighlight()
lastHoveredBtn = nil
end
isHoveringDisplay = false
end
return true
end
})
function onDestroy()
if tts ~= nil then
tts.stop()
tts.shutdown()
end
end
积分:9906