请问一下,我想用lua数组来存储一些字符串,每次获取到字符串之后就放在数组的第一个 元素中,其他字符串依次向后推。
然后在一个视图里面一次可以显示3条字符串,可以通过上下键翻阅存储的字符串,应该怎样做呢?
我自己写了一个,但是当数组里面的内容大于3条时,通过上下按键翻阅就会出现混乱的情况。求大佬指点,在线等,急。
这是我写的实现方法:
--E_MSG = {}; 定义
if(get_ctrl_attrib(wnd,"E_text1","text") ~= "" and first_E_txt == 0) then
E_MSG[1] = get_ctrl_attrib(wnd,"E_text1","text");
first_E_txt = 1;
end;
if(#E_MSG > 0) then
local e_msg_cc = #E_MSG;
if(e_msg_cc == 0) then
e_msg_cc = 1;
end;
if(E_MSG[1] ~= get_ctrl_attrib(wnd,"E_text1","text") and first_E_txt == 1) then
for i = e_msg_cc, 1, -1 do
if(E_MSG[i] ~= "" and E_MSG[i] ~= nil) then
E_MSG[i + 1] = E_MSG[i];
end;
end;
--ts.alert(#E_MSG); --#E_MSG值会一直往上加
E_MSG[1] = get_ctrl_attrib(wnd,"E_text1","text");
set_control_attrib(wnd, "E_text1", "text", E_MSG[1]);
set_control_attrib(wnd, "E_text2", "text", E_MSG[2]);
set_control_attrib(wnd, "E_text3", "text", E_MSG[3]);
if(E_MSG[i] == "") then
E_MSG[i] = nil;
end;
end;
end;
上面这个是将字符串存入数组中,但是上面这个存入好像也有问题,当我连续几次点击上下按键时,即使字符串没有增加,但是数组的长度还是会增加。比如说,一共有5条字符串,但是#E_MSG会变成7、8、9甚至更大。
--dw 定位当前被选中的位置(E_text1,E_text2,E_text3其中的一个)
--PageMark 数组的大小
--PageMark_2 当前数组中被选中的元素
if(control_name == "Downturn") then
if(PageMark == 0) then
PageMark = 1;
if(E_MSG[PageMark] ~= "") then
set_control_attrib(wnd,"E_text1","selected","true");
end;
elseif(PageMark == 2) then
if(dw == 0) then
dw = 1;
PageMark_2 = 1;
set_control_attrib(wnd,"E_text1","text",E_MSG[PageMark_2]);
set_control_attrib(wnd,"E_text2","text",E_MSG[PageMark_2 + 1]);
elseif(dw == 1 or dw == 2) then
dw = 2;
PageMark_2 = 2;
set_control_attrib(wnd,"E_text1","text",E_MSG[PageMark_2 - 1]);
set_control_attrib(wnd,"E_text2","text",E_MSG[PageMark_2]);
end
set_control_attrib(wnd,"E_text"..dw,"selected","true");
elseif(PageMark == 3) then
if(dw == 3 or dw == 2) then
dw = 3;
PageMark_2 = 3;
elseif(dw == 1) then
dw = 2;
PageMark_2 = 2;
elseif(dw == 0) then
dw = 1;
PageMark_2 = 1;
end;
set_control_attrib(wnd,"E_text1","text",E_MSG[PageMark - 2]);
set_control_attrib(wnd,"E_text2","text",E_MSG[PageMark - 1]);
set_control_attrib(wnd,"E_text3","text",E_MSG[PageMark]);
set_control_attrib(wnd,"E_text"..dw,"selected","true");
elseif(PageMark > 3) then
PageMark_2 = PageMark_2 + 1;
if(PageMark_2 > PageMark - 2) then
PageMark_2 = PageMark - 2;
end;
for i = 1, PageMark do
if(E_MSG[i] == "" or E_MSG[i] == nil) then
PageMark_2 = PageMark_2 - 1;
end;
end;
if(dw >= 3) then
dw = 3;
set_control_attrib(wnd,"E_text"..dw,"selected","true");
set_control_attrib(wnd,"E_text1","text",E_MSG[PageMark_2]);
set_control_attrib(wnd,"E_text2","text",E_MSG[PageMark_2 + 1]);
set_control_attrib(wnd,"E_text3","text",E_MSG[PageMark_2 + 2]);
elseif(dw < 3) then
dw = dw + 1;
set_control_attrib(wnd,"E_text"..dw,"selected","true");
end;
ts.alert(PageMark_2);
end;
end;
if(control_name == "Upturn") then
if(PageMark == 0) then
PageMark = 1;
if(E_MSG[PageMark] ~= "") then
set_control_attrib(wnd,"E_text"..dw,"selected","true");
end;
ts.alert("t0");
elseif(PageMark == 2) then
dw = 1;
PageMark_2 = 1;
set_control_attrib(wnd,"E_text1","text",E_MSG[PageMark_2]);
set_control_attrib(wnd,"E_text2","text",E_MSG[PageMark_2 + 1]);
set_control_attrib(wnd,"E_text"..dw,"selected","true");
elseif(PageMark == 3) then
if(dw == 3) then
dw = 2;
PageMark_2 = 2;
elseif(dw < 3) then
dw = 1;
PageMark_2 = 1;
end;
set_control_attrib(wnd,"E_text1","text",E_MSG[PageMark - 2]);
set_control_attrib(wnd,"E_text2","text",E_MSG[PageMark - 1]);
set_control_attrib(wnd,"E_text3","text",E_MSG[PageMark]);
set_control_attrib(wnd,"E_text"..dw,"selected","true");
elseif(PageMark > 3) then
if(dw == 3) then
dw = 2;
elseif( dw == 2 or dw == 1) then
dw = 1;
end;
PageMark_2 = PageMark_2 - 1;
if(PageMark_2 < 1) then
PageMark_2 = 1;
end;
set_control_attrib(wnd,"E_text1","text",E_MSG[PageMark_2]);
set_control_attrib(wnd,"E_text2","text",E_MSG[PageMark_2 + 1]);
set_control_attrib(wnd,"E_text3","text",E_MSG[PageMark_2 + 2]);
set_control_attrib(wnd,"E_text"..dw,"selected","true");
end;
end;
这是我写的通过上下按键来浏览字符串,反复在修改,这个也是正在修改的版本,一直有问题。