复选框根据文件进行检查并存储在as3中的文件中
Checkboxes to be checked based on the file and stored in the file in as3
我有 5 个复选框、1 个按钮和一个文本文件。在我的文本文件中,值存储如下:
1,0,0,0,1
当我运行程序时,第一个和最后一个复选框需要被选中,其他的需要被取消。
如果我选中或取消选中任何复选框然后单击保存按钮,更改的值必须更新到文本文件中。我怎样才能做到这一点?
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();
}
我有 5 个复选框、1 个按钮和一个文本文件。在我的文本文件中,值存储如下:
1,0,0,0,1
当我运行程序时,第一个和最后一个复选框需要被选中,其他的需要被取消。
如果我选中或取消选中任何复选框然后单击保存按钮,更改的值必须更新到文本文件中。我怎样才能做到这一点?
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();
}