最近我一直试图添加一个基本单词。我想这样做,所以你点击文本字段然后你键入你需要的然后它将它写入.txt文件(precreated与否)我不是很好的编码和我努力使用样本/其他人Stack Exchange要学习的问题。我所拥有的只是一些不能协同工作的代码,我不知道我需要做些什么来使它们工作(代码不是我的)
local textBox = native.newTextBox( 200, 200, 280, 140 )
textBox.text = "This is line 1.\nAnd this is line2"
textBox.isEditable = true
local file = io.open( filePath, "r" )
if file then
-- read all contents of file into a string
local contents = file:read( "*a" )
print( "Contents of " .. filePath )
print( contents )
io.close( file ) -- Important to close (python knowledge)
local t = display.newText( "Contents of ", 5, 80, nil, 16 ); -- w, h, ?, size
t:setFillColor( 1, 1, 135/255 ); -- edit
local t = display.newText( filePath, 5, 100, nil, 10 );
t:setFillColor( 1, 1, 135/255 );
local ylast = 130 -- how far down the Y value it can make words on the screen
for line in io.lines(filePath) do
local t = display.newText ( line, 15, ylast, nil, 14); -- dont understand
t:setFillColor( 1, 1, 1 );
ylast = ylast + 20
end
end
local function inputListener( event )
if event.phase == "began" then
-- user begins editing textBox
print( event.text )
elseif event.phase == "ended" then
textBox.text = event.text
local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory )
local file = io.open( path, "w" )
file:write( textBox.text )
io.close( file )
file = nil
elseif event.phase == "editing" then
print( event.newCharacters )
print( event.oldText )
print( event.startPosition )
print( event.text )
end
end
textBox:addEventListener( "userInput", inputListener )
答案 0 :(得分:1)
我建议使用loadsave函数,这样可以更轻松。
您可以从此处下载这些功能。
https://github.com/robmiracle/Simple-Table-Load-Save-Functions-for-Corona-SDK
下载后,只需将loadsave.lua文件放入主项目文件夹即可。
现在你可以像这样使用它。
示例:
local loadsave = require("loadsave")
local tableToSave = {}
-- load text into a table
tableToSave ['saved-text'] = textYouWantToSave
-- save text
loadsave.saveTable(tableToSave, "filename.json")
local savedTable = {}
-- load text
savedTable = loadsave.loadTable("filename.json")
-- text is stored here
savedTable['saved-text']