刚刚帮你写了一下,勉强能用,你可以再封装成一个类,这样就方便了
function setup()
-- Creates a button to clear the output
touched = false
myFrameCount = 0 --光标计数
textFieldX = 100
textFieldY = 600
textFieldWidth = 300
textFieldHeight = 30
end
function draw()
myFrameCount = myFrameCount +1
if (touchedInTextField(CurrentTouch.x,CurrentTouch.y)) then -- 判断是否点击到textField上
touched = true
else
touched = false
end
background(255)
stroke(200)
strokeWidth(2)
if touched then
fill(255)
rect(textFieldX,textFieldY,textFieldWidth,textFieldHeight)
-- 获取键盘输入内容
buffer = keyboardBuffer()
pushStyle()
textMode(CORNER)
fill(0)
text(buffer,textFieldX+3,textFieldY+4)
popStyle()
bufferLength = textSize(buffer)
if(myFrameCount < 10) then
pushStyle()
stroke(0)
strokeWidth(2)
fill(0)
line(textFieldX+3+bufferLength,textFieldY+3,textFieldX+3+bufferLength,textFieldY+27) --光标依据textField位置,文本长度定位
popStyle()
else
if (myFrameCount >30) then
myFrameCount = 0
end
end
showKeyboard()
else
fill(230)
rect(textFieldX,textFieldY,textFieldWidth,textFieldHeight)
hideKeyboard()
end
end
function touchedInTextField(x,y)
if (x<textFieldX) then return false end
if (x>textFieldX+textFieldWidth) then return false end
if (y<textFieldY) then return false end
if (y>textFieldY+textFieldHeight) then return falseend
return true
end