java me - How to highlight button when get focus on it in blackberry? -
i want make blackberry button images, 1 image should shown when focused , when unfocused.
or can blue color when focus off(by default) , red color when focus on, how can achieve such thing ?
see custom class
import net.rim.device.api.system.bitmap; import net.rim.device.api.ui.color; import net.rim.device.api.ui.field; import net.rim.device.api.ui.font; import net.rim.device.api.ui.fontfamily; import net.rim.device.api.ui.graphics; public class mybuttonfield extends field { // private int backgroundcolour = 0xffffff; private bitmap button; private bitmap on; //= bitmap.getbitmapresource("img/pink_scribble.bmp"); private bitmap off; // = bitmap.getbitmapresource("img/blue_scribble.bmp"); private int fieldwidth; private int fieldheight; // private int buffer = (display.getheight() - 105) / 2; private string text; private font fieldfont; private boolean btn_text = true; private static long style = field.field_hcenter | field.field_vcenter; public mybuttonfield(string _text, string onpath, string offpath) { super(field.focusable | style); text = _text; on = bitmap.getbitmapresource(onpath); off = bitmap.getbitmapresource(offpath); fieldwidth = on.getwidth(); fieldheight = on.getheight(); button = off; fieldfont = fieldfont(); } public mybuttonfield(string _text, string onpath, string offpath, string focusedpath){ this(_text, onpath, offpath); } public mybuttonfield(string _text, string onpath, string offpath, boolean btn_text) { this(_text, onpath , offpath); this.btn_text = btn_text; } public mybuttonfield(string _text, string onpath, string offpath, boolean btn_text, int style, int size) { this(_text, onpath , offpath, btn_text); fieldfont = fieldfont(style, size); } public mybuttonfield(string _text, string onpath, string offpath, long style) { this(_text, onpath , offpath); } // public mybuttonfield(string _text, string onpath, string offpath, int background) // { // this(_text, onpath , offpath); //// backgroundcolour = background; // } protected boolean navigationclick(int status, int time) { fieldchangenotify(1); return true; } protected void onfocus(int direction) { button = on; invalidate(); } protected void onunfocus() { button = off; invalidate(); } public int getpreferredwidth() { return fieldwidth; } public int getpreferredheight() { return fieldheight; } protected void layout(int arg0, int arg1) { setextent(getpreferredwidth(), getpreferredheight()); } public static font fieldfont() { try { fontfamily thefam = fontfamily.forname("system"); return thefam.getfont(net.rim.device.api.ui.font.plain, 14); } catch (classnotfoundexception ex) { ex.printstacktrace(); } return null; } public static font fieldfont(int style, int size) { try { fontfamily thefam = fontfamily.forname("system"); return thefam.getfont(style, size); } catch (classnotfoundexception ex) { ex.printstacktrace(); } return null; } protected void drawfocus(graphics graphics, boolean on) { if (on) { //graphics.setcolor(color.red); int width = getwidth(); int height = getheight(); graphics.drawbitmap(0, 0, fieldwidth, fieldheight, button, 0, 0); //draw border around field. /* graphics.fillroundrect(0, 0, 3, height,10,10); //left border graphics.fillroundrect(0, 0, width, 3,10,10); //top border graphics.fillroundrect(width-3, 0, 3, height,10,10); //right border graphics.fillroundrect(0, height-3, width, 3,10,10); //bottom border */ graphics.setcolor(color.gray); } invalidate(); } public void setfocusimage() { button = on; } public void setunfocusimage() { button = off; } protected void fieldchangenotify(int context) { this.getchangelistener().fieldchanged(this, context); } protected void paint(graphics graphics) { graphics.drawbitmap(0, 0, fieldwidth, fieldheight, button, 0, 0); graphics.setfont(getfont().derive(font.plain, 8)); graphics.setcolor(color.lightgray); // graphics.fillroundrect(0, 0, fieldwidth, fieldheight, 8, 8); // graphics.setcolor(color.gray); //graphics.drawroundrect(0, 0, fieldwidth, fieldheight, 8, 8); if(btn_text) graphics.drawtext(text, 10, 5); } public string getlabel() { return text; } }
you should override onfocus()
, onunfocus()
, paint()
.
please refer custom button field article.
Comments
Post a Comment