我有5个复选框,1个按钮和一个文本文件。在我的文本文件中,值的存储方式如下:
1,0,0,0,1
当我运行程序时,需要检查第一个和最后一个复选框,而其他复选框需要取消选中。
如果我选中或取消选中任何复选框,则单击“保存”按钮,必须在文本文件中更新更改的值。我怎样才能做到这一点?
答案 0 :(得分:0)
var filename:String = "checkboxes.txt";
var checkboxes:Vector.<CheckBox> = new <CheckBox>[checkbox1, checkbox2, etc];
function load():void {
// read text file
var file:File = File.applicationStorageDirectory.resolvePath(filename);
if(!file.exists)
return;
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var text:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
fileStream.close();
// decode text values to checkboxes
var values:Array = text.split(",");
for(var i:int = 0; i < values.length; i++){
var checked:Boolean = values[i] == "1";
checkboxes[i].selected = checked;
}
}
function save():void {
// encode checkboxes to text
var values:Array = [];
for(var i:int = 0; i < checkboxes.length; i++){
values.push(int(checkboxes[i].selected));
}
var text:String = values.join(",");
// write to file
var file:File = File.applicationStorageDirectory.resolvePath(filename);
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeUTFBytes(text);
fileStream.close();
}