2

我的问题是如何将用户文本输入保存到 corona sdk 中的 .txt 文件中。

所以基本上我想要实现的是,当用户在文本框中写入内容然后按下保存按钮时,来自 textxbox 的文本会保存到系统文档目录中的 .txt 文件中。

谢谢帮忙!:)

这是代码:)

local function inputListener( event )
    if event.phase == "began" then

        -- user begins editing textBox
        print( event.text )

    elseif event.phase == "ended" then

       local myfile = io.open( filePath, "w" )  
       myfile:write(textBox)  
       io.close(myfile)  



    elseif event.phase == "editing" then

        print( event.newCharacters )
        print( event.oldText )
        print( event.startPosition )
        print( event.text )

    end
end

local textBox = native.newTextBox( 200, 200, 280, 140 )
textBox.text = "This is line 1.\nAnd this is line2"
textBox.isEditable = true
textBox:addEventListener( "userInput", inputListener )
4

1 回答 1

2

我建议您在此处阅读 Corona Labs 提供的文档:http: //docs.coronalabs.com/guide/data/readWriteFiles/index.html

这是文档中的合并代码和您提供的代码。使用此代码,我无法复制您在评论中提到的错误,因此我无法为您提供进一步帮助。

local textBox = native.newTextBox( 200, 200, 280, 140 )
textBox.text = "This is line 1.\nAnd this is line2"
textBox.isEditable = true

local function inputListener( event )
    if event.phase == "began" then

        -- user begins editing textBox
        print( event.text )

    elseif event.phase == "ended" then

       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 )
于 2014-06-25T19:46:19.473 回答