从文件读入文本框电晕

时间:2013-05-12 21:01:16

标签: textbox corona readfile

我想从一个文件读到文本框,用户可以在其中添加更多数据然后它将作为短信

我该怎么做

我尝试了一些现成的代码,但始终无法正常工作

enter code here

----------------------------------------------- ----------------------------------

- scence4-1.lua


 local storyboard = require( "storyboard" )
 local scene = storyboard.newScene()
 local widget = require( "widget" )

---------------------------------------------------------------------------------

- 开始实施

   local obj2 = display.newImage( "images/bg4-1.png")

   local btn1 = display.newImage( "images/btn_send.png",151,415 )

local btn2 = display.newImage(“images / btn_cancel.png”,6,415)     btn1.id =“发送对象”


local myname = GV_Name    local myid = GV_ID_NO

print (myname)
print(myid)

function scene:enterScene( event )
local params = event.params

print (params.GV_Name)
print (params.GV_ID_NO)

end





local path = system.pathForFile( "data.txt",  system.DocumentsDirectory  )
local fh_note = io.open( path, "r" )
if fh_note then
    print (path)
    io.close( fh_note )
end

        textBox = native.newTextBox(25, 95, 265, 305 )

    print(textBox.text)

    textBox:setReferencePoint(display.TopCenterReferencePoint)
    --textBox.x = screenCenter
    textBox.size = 16
    textBox.isEditable = true



    local my_text = textBox.text
-- onRelease listener for 'sendSMS' button
local function onSendSMS( event )
-- compose an SMS message (doesn't support attachments)
local options =
{
   to = { "0505930118", "9876543210" },
   body = my_text
}
native.showPopup("sms", options)

-- NOTE: options table (and all child properties) are optional
end

local function onbtn1Touch( event )
 if event.phase == "began" then
print (my_text)
    print( "Touch event began on: " .. event.target.id )
        --onRelease = onSendSMS
end
return true
end
btn1:addEventListener( "touch", onSendSMS )``




return scene
enter code here

1 个答案:

答案 0 :(得分:0)

尝试以下行来阅读文件:

local path = system.pathForFile( "data.txt", system.DocumentsDirectory )
local file = io.open( path, "r" )
if file then
   local contents = file:read( "*a" )
   print(contents) -- data from file, use this as input to textbox
   io.close( file )
end

继续编码.........:)