我正在为 Blackberry 编写一个应用程序,但遇到以下问题:
我通过扩展 Manager 类实现了自定义字段。该字段非常简单,由一个标签和两个复选框(是和否)组成。字段的布局使标签位于左侧并使用尽可能多的空间,同时仍然为复选框留出空间,这些复选框位于右侧并与标签相比垂直居中(如果跨越多于一行)。所以它看起来像这样:
这是一个问题? O 是 O 否
到目前为止,在布局字段方面一切都很好。但现在我正在尝试处理焦点遍历。首先,当用户向上或向下滚动时,默认行为是在"is"和“否”选项之间移动。我想在按下向上或向下时移动到上方或下方的下一个字段,所以我这样做了:
protected int moveFocus(int amount, int status, int time) {
if (status == 537001984 || status == -1610481664) { // Up or down was pressed
// Don't move focus between yes and no when up or down is pressed.
return amount;
}
return super.moveFocus(amount, status, time);
}
这似乎有效。
我想做的下一件事是记住当字段失去焦点时哪个选项最后获得焦点,然后再次获得焦点时(无论焦点来自哪个方向)设置该字段以获得焦点。我尝试覆盖 onUnfocus 和 onFocus,以便 onUnfocus 我注意到哪个字段被聚焦,然后 onFocus setFocus 到该字段。但我得到了一个 StackOverflowError,我猜是因为在管理器内的一个字段上调用 setFocus 实际上再次调用了管理器本身的 onFocus?
那么有人知道我应该怎么做吗?我检查了 DateField,它具有我正在寻找的确切行为,即,它会记住您上次是在日、月或年字段中,并在字段本身获得焦点时将此字段设置为焦点。
最佳答案
当我需要在 onFocus 内纠正焦点时,我使用一些标志:
boolean isCustomFocusSet = false;
protected void onFocus(int direction) {
if (!isCustomFocusSet) {
isCustomFocusSet = true;
int fieldIndex = getLastFocusedCBIndex();
CustomField field = (CustomField)getField(fieldIndex);
field.setFocus();
} else {
isCustomFocusSet = false;
super.onFocus(direction);
}
}
更新 对于 CustomField,这可能会解析为调用 this.onFocus 而不是 super.onFocus:
CustomField field = (CustomField)getField(fieldIndex);
关于java - 黑莓:如何正确处理自定义字段中的焦点遍历?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2673976/