gpt4 book ai didi

java-me - 如何在 Canvas 中创建多个复选框

转载 作者:行者123 更新时间:2023-12-04 06:08:55 26 4
gpt4 key购买 nike

当我尝试在 Canvas 中创建复选框时遇到问题。

我的复选框运行良好,但我不知道如何存储每个项目的值,这意味着当用户检查第 1 行,然后他们移动到另一行复选框仍然检查第 1 行,当用户检查第 1 行和第 2 行并移动时到另一行,复选框将选中第 1 行和第 2 行。

但我找不到这个问题的解决方案

最佳答案

修改您的代码以使用 selectTodelete作为 bool 数组而不是int,大约如下所示

    // ...initialization of DataList
boolean[] selectTodelete = new boolean[2]; // instead of int
{ selectTodelete[0] = selectTodelete[1] = false; } // init array
Command editCommand, backCommand,selectCmd, unselectCmd,selectAll;
//...

protected void paint(Graphics g) {
//...
for(int i =0 ; i<countRow; i++ ){
//draw background
//...
if(selectTodelete[i]){ // was selectTodelete == 1
//draw select dot at location for row 'i'
//...
}
// remove: you don't need that anymore: if(selectTodelete == 2) {
//draw select dot...
//}

// draw a checkbox before each item
// ...
}
}

public void commandAction(Command c, Displayable d) {
//...
if(c == selectCmd){
selectTodelete[selectedItem] = true;
}
if(c== unselectCmd){
selectTodelete[selectedItem] = false;
}
if(c == selectAll){
selectTodelete[0] = selectTodelete[1] = true;
}
repaint();
}
//...
}

更新 - 在评论中回答问题

I want to get RCID fit to checked it mean when row was checked I can get this id and when I use delete command it will delete all rows were checked



为此,您可以公开 selectTodelete用于在其类之外与 getter 一起使用,或者更好的是,使用如下方法...
    boolean isSelected(int elementNum) {
return elementNum >= 0 && elementNum < selectTodelete.length
&& selectTodelete[elementNum];
} // modelled after javax.microedition.lcdui.Choice.isSelected

...当您需要处理 RCID 时,可以在任何地方进一步使用这样公开的信息,例如在下面的方法中:
    Vector useSelection(DataList dataList, DataStore[][] ds) {
Vector result = new Vector();
int count = ds.length;
for(int i = 0; i < count; i++ ) {
if (!dataList.isSelected(i)) {
continue; // skip non selected
}
System.out.println("RCID selected: [" + ds[i][5].cellText + "]");
result.addElement(ds[i][5]);
}
return result;
}

关于java-me - 如何在 Canvas 中创建多个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8011957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com