问题描述
我有一个VerticalFieldManager和VFM在HorizontalFieldManager一个的ImageButton。当我在现场周围的任意位置单击ImageButton的单击事件被触发。我想这click事件只火单击ImageButton的,而不是场(VFM或HFM)时。
以下是code:
的ImageButton登录=新的ImageButton(configModel.getLoginButton(),可聚焦| Field.FIELD_RIGHT,login.png, plogin.png,0x9cbe95);
HorizontalFieldManager HFM =新HorizontalFieldManager(USE_ALL_WIDTH);
VerticalFieldManager loginButtonVfm =新VerticalFieldManager(USE_ALL_WIDTH);loginButtonVfm.add(登录);
hfm.add(loginButtonVfm);
加(HFM);
FieldChangeListener loginListener =新FieldChangeListener(){
公共无效fieldChanged(场场,诠释上下文){
Dialog.alert(炒鱿鱼);
}
}
Login.setChangeListener(loginListener);
的ImageButton类code是在这里:
公共类ImageButton的扩展字段{ //图像按钮类
私人字符串_label;
私人诠释_labelHeight;
私人诠释_labelWidth;
私人字体_font; 私人位图_currentPicture;
私人位图_onPicture;
私人位图_offPicture;
INT色; 公众的ImageButton(字符串文本,长款式,IMG的字符串,字符串img_hvr,诠释颜色){
超(样式); _offPicture = Bitmap.getBitma $ P $的PSource(IMG);
_onPicture = Bitmap.getBitma $ P $的PSource(img_hvr); 。_font = Font.getDefault()获得(Font.BOLD,7,Ui.UNITS_pt);
_label =文本;
_labelHeight = _onPicture.getHeight();
_labelWidth = _onPicture.getWidth(); this.color =颜色; _currentPicture = _offPicture;
}
公共无效setImage(字符串IMG){ _offPicture = Bitmap.getBitma $ P $的PSource(IMG);
_currentPicture = _offPicture;
} / **
返回:按钮上的文本
* /
公共无效的setText(字符串文本){
_label =文本;
}
字符串的getText(){
返回_label;
} / **
*外地执行。
* @see net.rim.device.api.ui.Field#得到preferredHeight()
* /
公众诠释的get preferredHeight(){
返回_labelHeight;
}
/ **
*外地执行。
* @see net.rim.device.api.ui.Field#得到preferredWidth()
* /
公众诠释的get preferredWidth(){
返回_labelWidth;
} / **
*外地执行。在获得焦点时更改画面。
* @see net.rim.device.api.ui.Field#的onfocus(INT)
* /
保护无效的onfocus(INT方向){ _currentPicture = _onPicture;
//无效();
super.onFocus(方向);
} / **
*外地执行。更改图片回来时,焦点将丢失。
* @see net.rim.device.api.ui.Field#onUnfocus()
* /
保护无效onUnfocus(){
_currentPicture = _offPicture;
无效();
super.onUnfocus();
} / **
*外地执行。
* @see net.rim.device.api.ui.Field#DRAWFOCUS(图形,布尔)
* /
//保护无效DRAWFOCUS(图形显卡,布尔){
// // 没做什么
//}
保护无效DRAWFOCUS(图形显卡,布尔){
如果(上){
//绘制自己的自定义焦点。
}
}
/ **
*外地执行。
* @see net.rim.device.api.ui.Field#布局(INT,INT)
* /
保护无效布局(INT宽度,高度INT){
setExtent(Math.min(宽度,GET preferredWidth()),
Math.min(身高,GET preferredHeight()));
}
/ **
*外地执行。
* @see net.rim.device.api.ui.Field#漆(图形)
* /
保护无效漆(图形图像){
//首先绘制背景颜色和图像
graphics.setColor(this.color);
graphics.fillRect(0,0,的getWidth(),的getHeight());
graphics.drawBitmap(0,0,的getWidth(),的getHeight(),_currentPicture,0,0); //然后绘制文本
graphics.setColor(Color.WHITE);
graphics.setFont(_font);
graphics.setFont(graphics.getFont()获得(Font.BOLD));
graphics.drawText(_label,5,9,
(中间体)(对getStyle()及DrawStyle.ELLIPSIS | DrawStyle.VALIGN_MASK | DrawStyle.HALIGN_MASK),
的getWidth() - 6); } / **
*重写,这样的事件调度线程可以赶上这个活动
*而不是让它在这里抓到..
* @see net.rim.device.api.ui.Field#navigationClick(INT,INT)
* /
保护布尔navigationClick(INT的地位,诠释时间){
fieldChangeNotify(1);
返回true;
}
}
这种事情实际上是pretty容易找到错误,当您尝试编写自定义字段自己。 (这不是你的错......黑莓Java使得这个东西比较难比它真的应该!)的
我觉得解决这个问题最简单的方法是重用的东西,别人已经测试。我建议在看黑莓高级UI 库,特别是这两个类:
BaseButtonField
BitmapButtonField
当我觉得他们做的正是你需要的。我认为重要的code您需要更改或添加到您的的ImageButton
类是这样的:
保护布尔keyChar(CHAR字符,诠释状态,诠释时间)
{
如果(字符== Characters.ENTER){
clickButton();
返回true;
}
返回super.keyChar(文字,状态,时间);
}保护布尔navigationClick(INT的地位,诠释时间)
{
如果(状态!= 0)clickButton();
返回true;
}保护布尔trackwheelClick(INT的地位,诠释时间)
{
如果(状态!= 0)clickButton();
返回true;
}保护布尔invokeAction(INT行动)
{
开关(动作){
案例ACTION_INVOKE:{
clickButton();
返回true;
}
}
返回super.invokeAction(动作);
}保护布尔的TouchEvent(的TouchEvent消息)
{
INT X = message.getX(1);
INT Y = message.getY(1);
如果(X℃,||ý℃,|| X - GT; getExtent()宽度||ý方式>。getExtent()高度){
//外场
返回false;
}
开关(message.getEvent()){ 案例TouchEvent.UNCLICK:
clickButton();
返回true;
}
返回super.touchEvent(消息);
}/ **
*一个公开的方式来点击这个按钮
* /
公共无效clickButton()
{
fieldChangeNotify(0);
}
然后,您就可以使用 FieldChangeListener
完全按照你现在的样子,但除非点击出现的在您的按钮图像按钮将不会把它叫做。
如果你愿意,你当然可以改变上面的code使用
fieldChangeNotify(1);
如果 1
是一个有意义的背景的为你的价值。最重要的是添加的其他方法,并更改 navigationClick()
。
I have a ImageButton in a VerticalFieldManager and VFM in a HorizontalFieldManager. The ImageButton click event is firing when I click anywhere in the surrounding field. I want that click event to only fire when clicking the ImageButton, not the field (VFM or HFM).
Following is the code:
ImageButton Login = new ImageButton(configModel.getLoginButton(), FOCUSABLE|Field.FIELD_RIGHT, "login.png", "plogin.png",0x9cbe95);
HorizontalFieldManager hfm = new HorizontalFieldManager(USE_ALL_WIDTH);
VerticalFieldManager loginButtonVfm = new VerticalFieldManager(USE_ALL_WIDTH);
loginButtonVfm.add(Login);
hfm.add(loginButtonVfm);
add(hfm);
FieldChangeListener loginListener = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
Dialog.alert("Fired");
}
}
Login.setChangeListener(loginListener);
ImageButton Class Code is here:
public class ImageButton extends Field{
//Image Button Class
private String _label;
private int _labelHeight;
private int _labelWidth;
private Font _font;
private Bitmap _currentPicture;
private Bitmap _onPicture;
private Bitmap _offPicture;
int color;
public ImageButton(String text, long style ,String img, String img_hvr, int color){
super(style);
_offPicture = Bitmap.getBitmapResource(img);
_onPicture = Bitmap.getBitmapResource(img_hvr);
_font = Font.getDefault().derive(Font.BOLD, 7, Ui.UNITS_pt);
_label = text;
_labelHeight = _onPicture.getHeight();
_labelWidth = _onPicture.getWidth();
this.color = color;
_currentPicture = _offPicture;
}
public void setImage(String img){
_offPicture = Bitmap.getBitmapResource(img);
_currentPicture = _offPicture;
}
/**
* @return The text on the button
*/
public void setText(String text){
_label = text;
}
String getText(){
return _label;
}
/**
* Field implementation.
* @see net.rim.device.api.ui.Field#getPreferredHeight()
*/
public int getPreferredHeight(){
return _labelHeight;
}
/**
* Field implementation.
* @see net.rim.device.api.ui.Field#getPreferredWidth()
*/
public int getPreferredWidth(){
return _labelWidth;
}
/**
* Field implementation. Changes the picture when focus is gained.
* @see net.rim.device.api.ui.Field#onFocus(int)
*/
protected void onFocus(int direction) {
_currentPicture = _onPicture;
// invalidate();
super.onFocus(direction);
}
/**
* Field implementation. Changes picture back when focus is lost.
* @see net.rim.device.api.ui.Field#onUnfocus()
*/
protected void onUnfocus() {
_currentPicture = _offPicture;
invalidate();
super.onUnfocus();
}
/**
* Field implementation.
* @see net.rim.device.api.ui.Field#drawFocus(Graphics, boolean)
*/
// protected void drawFocus(Graphics graphics, boolean on) {
// // Do nothing
// }
protected void drawFocus(Graphics graphics, boolean on) {
if (on) {
//draw your own custom focus.
}
}
/**
* Field implementation.
* @see net.rim.device.api.ui.Field#layout(int, int)
*/
protected void layout(int width, int height) {
setExtent(Math.min( width, getPreferredWidth()),
Math.min( height, getPreferredHeight()));
}
/**
* Field implementation.
* @see net.rim.device.api.ui.Field#paint(Graphics)
*/
protected void paint(Graphics graphics){
// First draw the background colour and picture
graphics.setColor(this.color);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);
// Then draw the text
graphics.setColor(Color.WHITE);
graphics.setFont(_font);
graphics.setFont(graphics.getFont().derive(Font.BOLD));
graphics.drawText(_label, 5,9,
(int)( getStyle() & DrawStyle.ELLIPSIS | DrawStyle.VALIGN_MASK | DrawStyle.HALIGN_MASK),
getWidth() - 6 );
}
/**
* Overridden so that the Event Dispatch thread can catch this event
* instead of having it be caught here..
* @see net.rim.device.api.ui.Field#navigationClick(int, int)
*/
protected boolean navigationClick(int status, int time){
fieldChangeNotify(1);
return true;
}
}
This kind of thing is actually pretty easy to find bugs in, when you try to write custom fields yourself. (It's not your fault ... BlackBerry Java makes this stuff more difficult than it really should be!)
I think the easiest way to solve this problem is to reuse something that someone else has already tested. I would recommend looking at the BlackBerry Advanced UI library, in particular, these two classes:
As I think they do exactly what you need. I believe the important code you need to change, or add to your ImageButton
class is this:
protected boolean keyChar( char character, int status, int time )
{
if( character == Characters.ENTER ) {
clickButton();
return true;
}
return super.keyChar( character, status, time );
}
protected boolean navigationClick( int status, int time )
{
if (status != 0) clickButton();
return true;
}
protected boolean trackwheelClick( int status, int time )
{
if (status != 0) clickButton();
return true;
}
protected boolean invokeAction( int action )
{
switch( action ) {
case ACTION_INVOKE: {
clickButton();
return true;
}
}
return super.invokeAction( action );
}
protected boolean touchEvent( TouchEvent message )
{
int x = message.getX( 1 );
int y = message.getY( 1 );
if( x < 0 || y < 0 || x > getExtent().width || y > getExtent().height ) {
// Outside the field
return false;
}
switch( message.getEvent() ) {
case TouchEvent.UNCLICK:
clickButton();
return true;
}
return super.touchEvent( message );
}
/**
* A public way to click this button
*/
public void clickButton()
{
fieldChangeNotify( 0 );
}
You will then be able to use a FieldChangeListener
exactly as you are now, but the image button will not call it unless the click occurs on your button.
If you want, you can certainly change the code above to use
fieldChangeNotify( 1 );
if 1
is a meaningful context value for you. The important thing is to add the other methods, and change navigationClick()
.
这篇关于黑莓单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!