我想在通过扩展 Field 类创建的两个自定义字段上捕获事件。事件应该只是触摸事件。
一个领域使用graphics.drawRect(10,10,20,20)
和其他领域使用graphics.drawRect(50,50,20,20)
。
(我不会使用硬编码的值,但在这里写它们只是为了举例)。
我应该能够单独捕获事件;这意味着不同领域的不同事件。
我的代码如下所示: 主屏幕类:
package mypackage;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.container.MainScreen;
public final class MyScreen extends MainScreen
{
CustomFieldManager cfm;
public MyScreen()
{
cfm=new CustomFieldManager();
CustomButtonField cb=new CustomButtonField(Field.FOCUSABLE,20,20,40,40);
CustomButtonField cb1=new CustomButtonField(Field.FOCUSABLE,70,70,40,40);
new CustomButtonField(cb,cb1);
cfm.add(cb);
cfm.add(cb1);
add(cfm);
}
}
Field class:
package mypackage;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.Dialog;
public class CustomButtonField extends Field {
int a,b,c,d;
CustomButtonField c1,c2;
public CustomButtonField(long style,int a,int b,int c,int d){
super(style);
this.a=a;
this.b=b;
this.c=c;
this.d=d;
}
public CustomButtonField(Object o1,Object o2){
c1=(CustomButtonField)o1;
c2=(CustomButtonField)o2;
}
public void movefirst(){
Dialog.alert("god");
}
protected void drawFocus(Graphics graphics, boolean on) {
}
protected void layout(int width, int height) {
setExtent(200, 200);
}
protected void paint(Graphics graphics) {
graphics.drawRect(a, b, c, d);
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
}
and manager class :
package mypackage;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.Dialog;
public class CustomFieldManager extends Manager {
CustomButtonField first, second;
public CustomFieldManager() {
super(Manager.NO_HORIZONTAL_SCROLL | Manager.NO_VERTICAL_SCROLL);
}
protected void sublayout(int arg0, int arg1) {
int numberOfFields = getFieldCount();
for (int i = 0; i < numberOfFields; i++) {
// Get the field.
first = (CustomButtonField) getField(i);
setPositionChild(first, 0, 0);
layoutChild(first, 110, 110);
}
setExtent(200, 200);
}
setPositionChild(first, 50, 50);
invalidate();
}
}