Number Field yang di maksud disini adalah field yang hanya menerima inputan berupa angka (0-9). Code-nya sebenarnya hampir sama dengan Custom EditField yang pernah saya tulis, tapi ada beberapa perbedaan.
Berikut code-nya:
Berikut code-nya:
import net.rim.device.api.system.Characters; import net.rim.device.api.system.Display; 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.Graphics; import net.rim.device.api.ui.Manager; import net.rim.device.api.ui.component.BasicEditField; public class NumberFieldCustom extends Manager { private final static int DEFAULT_LEFT_MARGIN = 1; private final static int DEFAULT_RIGHT_MARGIN = 1; private final static int DEFAULT_TOP_MARGIN = 3; private final static int DEFAULT_BOTTOM_MARGIN = 1; private final static int DEFAULT_LEFT_PADDING = 5; private final static int DEFAULT_RIGHT_PADDING = 5; private final static int DEFAULT_TOP_PADDING = 3; private final static int DEFAULT_BOTTOM_PADDING = 3; private int topMargin = DEFAULT_TOP_MARGIN; private int bottomMargin = DEFAULT_BOTTOM_MARGIN; private int leftMargin = DEFAULT_LEFT_MARGIN; private int rightMargin = DEFAULT_RIGHT_MARGIN; private int topPadding = DEFAULT_TOP_PADDING; private int bottomPadding = DEFAULT_BOTTOM_PADDING; private int leftPadding = DEFAULT_LEFT_PADDING; private int rightPadding = DEFAULT_RIGHT_PADDING; private int totalHorizontalEmptySpace = leftMargin + leftPadding + rightPadding + rightMargin; private int totalVerticalEmptySpace = topMargin + topPadding + bottomPadding + bottomMargin; private int minHeight = 21 + totalVerticalEmptySpace; private int width; private int height = minHeight; private int text_colour = 0xFF0000; private int border_color = 0x000066; private int background_color = 0xFFFF99; private String label = ""; private Font headingFont = getFont().derive(Font.PLAIN, 20); private BasicEditField editField; public NumberFieldCustom (){ super(0); this.label = ""; this.width = Display.getWidth()/2; editField = new BasicEditField(BasicEditField.FILTER_NUMERIC); add(editField); } public NumberFieldCustom (int panjang){ super(0); this.label = ""; this.width = panjang; editField = new BasicEditField(BasicEditField.FILTER_NUMERIC); add(editField); } protected void sublayout(int width, int height) { Field field = getField(0); layoutChild(field, this.width - totalHorizontalEmptySpace , this.height - totalVerticalEmptySpace); setPositionChild(field, leftMargin+leftPadding, topMargin+topPadding); setExtent(this.width, this.height); } protected void paint(Graphics graphics){ graphics.setColor(border_color); graphics.drawRoundRect(leftMargin, topMargin, width - (leftMargin+rightMargin), height - (topMargin+bottomMargin), 7, 7); graphics.setColor(background_color); graphics.fillRoundRect(leftMargin+1, topMargin+1, (width - (leftMargin+rightMargin))-2, (height- (topMargin+bottomMargin))-2, 7, 7); graphics.setColor(Color.BLACK); if(headingFont!=null){ setFont(headingFont); graphics.drawText(label, 2, 10); }else{ graphics.drawText(label, 2, 7); } graphics.setColor(text_colour); BasicEditField ef = (BasicEditField)getField(0); String entireText = ef.getText(); boolean longText = false; String textToDraw = ""; Font font = headingFont; int availableWidth = width - totalHorizontalEmptySpace; if (font.getAdvance(entireText) <= availableWidth) { textToDraw = entireText; } else { int endIndex = entireText.length(); for (int beginIndex = 1; beginIndex < endIndex; beginIndex++) { textToDraw = entireText.substring(beginIndex); if (font.getAdvance(textToDraw) <= availableWidth) { longText = true; break; } } } if (longText == true) { ef.setText(textToDraw); super.paint(graphics); ef.setText(entireText); } else { super.paint(graphics); } } public int getPreferredWidth(){ return width; } public int getPreferredHeight(){ return height; } protected boolean keyChar(char ch, int status, int time){ if (ch == Characters.ENTER) { return true; } else { return super.keyChar(ch, status, time); } } protected void onUnfocus() { super.onUnfocus(); } protected void onFocus(int direction) { super.onFocus(direction); } protected void drawFocus(Graphics graphics, boolean on) { super.drawFocus(graphics, on); } public String getText(){ return ((BasicEditField) getField(0)).getText(); } public void setText(final String text){ ((BasicEditField)getField(0)).setText(text); } }
No comments:
Post a Comment
Silahkan Isi komentar anda,thanks!