问题
I have created a ButtonField & a BitmapField like..
public class MyCanvas extends MainScreen implements FieldChangeListener
{
HorizontalFieldManager hfm;
private Bitmap startBitmap;
private BitmapField startBitmapField;
private ButtonField okButton;
MyCanvas()
{
hfm = new HorizontalFIeldManager();
startBitmap = Bitmap.getBitmapResource("start.png");
startBitmapField = new BitmapField(startBitmap);
startBitmapField.setChangeListener(this);
hfm.add(startBitmapField);
okButton = new ButtonField("Ok", ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY);
okButton.setChangeListener(this);
hfm.add(okButton);
}
public void fieldChanged(Field field, int context)
{
if(field == startBitmapField)
{
System.out.println("Touched START...");
}
else if(field == okButton)
{
System.out.println("Touched Ok...");
}
}
}
But the ButtonField or BitmapField click is not happening in Blackberry 4.7 simulator.
I want to build it for Blackberry Storm so I m using Blackberry 4.7
How to handle click/touch events for ButtonField & BitmapField for Blackberry Storm?
I m creating the ButtonField & BitmapFields as
okButtonField = new ButtonField("Ok", BitmapField.HIGHLIGHT_SELECT | BitmapField.FOCUSABLE);
startBitmapField = new BitmapField(startBitmap, BitmapField.HIGHLIGHT_SELECT | BitmapField.FOCUSABLE);
Its working with..
protected boolean touchEvent(TouchEvent event)
{
switch( event.getEvent() )
{
case TouchEvent.DOWN: ........
return true;
case TouchEvent.MOVE: .......
return true;
case TouchEvent.UP: ........
return true;
case TouchEvent.CLICK:
if(deleteButton.isFocus())
{
System.out.println("Touched DEL ..........");
}
else if(okButton.isFocus())
{
System.out.println("Touched OK ..........");
}
else if(startBitmapField.isFocus())
{
System.out.println("Touched START ..........");
}
return true;
}
return false;
}
but everytime the same button is invoked which is having focus.
Means if "Ok" button is having focus then even though u clicked on "Delete" button "Ok" button is called.
So how to change the focus on Button Click? means whichever ButtonField or BitmapField is clicked, should get the focus?
is there any method to check "button.isClicked() like button.isFocus() " ?
回答1:
First of all, don't forget to add hfm to screen ;)
Actually button click works fine.
Now, to make bitmap click works as well, implement protected boolean touchEvent(TouchEvent message) for your BitmapField. It will be better to create extended class:
class MyCanvas extends MainScreen implements FieldChangeListener {
HorizontalFieldManager hfm;
private Bitmap startBitmap;
private BitmapField startBitmapField;
private ButtonField okButton;
private ButtonField cancelButton;
MyCanvas() {
hfm = new HorizontalFieldManager();
add(hfm);
startBitmap = Bitmap.getBitmapResource("start.png");
startBitmapField = new TouchBitmapField(startBitmap);
startBitmapField.setChangeListener(this);
hfm.add(startBitmapField);
okButton = new ButtonField("Ok", ButtonField.CONSUME_CLICK
| ButtonField.NEVER_DIRTY);
okButton.setChangeListener(this);
hfm.add(okButton);
cancelButton = new ButtonField("Cancel", ButtonField.CONSUME_CLICK
| ButtonField.NEVER_DIRTY);
cancelButton.setChangeListener(this);
hfm.add(cancelButton);
}
public void fieldChanged(Field field, int context) {
if (field == startBitmapField) {
System.out.println("Touched START...");
} else if (field == okButton) {
System.out.println("Touched Ok...");
} else if (field == cancelButton) {
System.out.println("Touched Cancel...");
}
}
}
class TouchBitmapField extends BitmapField {
public TouchBitmapField(Bitmap startBitmap) {
super(startBitmap);
}
protected boolean touchEvent(TouchEvent message) {
if (TouchEvent.CLICK == message.getEvent()) {
FieldChangeListener listener = getChangeListener();
if (null != listener)
listener.fieldChanged(this, 1);
}
return super.touchEvent(message);
}
}
来源:https://stackoverflow.com/questions/1351101/how-to-handle-buttonfield-bitmapfield-click-touch-events-in-blackberry-storm