我想要一个带圆角边框和白色背景填充的BasicEditField
。
这是我的代码
public class BasicField extends BasicEditField {
XYEdges padding = new XYEdges(0,0,0,0);
int color = Color.BLACK;
int lineStyle = Border.STYLE_SOLID;
int Width, Height;
Border roundedBorder = BorderFactory.createRoundedBorder(padding, color, lineStyle);
public BasicField()
{
super(BasicEditField.NO_NEWLINE);
//this.setPadding(2, 2, 2, 2);
//this.setBorder(roundedBorder);
}
public int getPreferredWidth()
{
int labelWidth = getFont().getAdvance(getLabel()) - 1;
Width = Graphics.getScreenWidth() -150;
return Width;
}
public int getPreferredHeight()
{
return 10;
}
public void paint(Graphics g) {
int currCol = g.getColor();
g.setBackgroundColor( Color.WHITE );
g.fillRect(0, 0, getPreferredWidth(), getPreferredHeight() );
g.clear();
g.setColor( Color.NAVY );
super.paint( g );
}
protected void layout( int maxWidth, int maxHeight )
{
super.layout( getPreferredWidth(), getPreferredHeight() );
}
}
答案 0 :(得分:2)
你去吧 -
截图:
border.png:
MyEdit.java:
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
public class MyEdit extends UiApplication {
public static void main(String args[]) {
MyEdit app = new MyEdit();
app.enterEventDispatcher();
}
public MyEdit() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
Border myBorder = BorderFactory.createBitmapBorder(
new XYEdges(20, 16, 27, 23),
Bitmap.getBitmapResource("border.png"));
BasicEditField myField = new BasicEditField(TextField.NO_NEWLINE) {
protected void paint(Graphics g) {
if (getTextLength() == 0) {
g.setColor(Color.LIGHTGRAY);
g.drawText("Search", 0, 0);
}
g.setColor(Color.BLACK);
super.paint(g);
}
};
public MyScreen() {
myField.setBorder(myBorder);
setTitle(myField);
}
}
答案 1 :(得分:0)
class BasicEditScreen extends MainScreen
{
public BasicEditScreen()
{
add(new BasicField(200, 40, FIELD_LEFT, "Enter here..", "horizontal"));
}
}
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
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.EditField;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class BasicField extends Manager
{
private int managerWidth;
private int managerHeight;
private int inactiveBorderColor = Color.GRAY;
private int activeBorderColor = Color.GREENYELLOW;
private int borderColor = inactiveBorderColor;
private int backgroundColor = Color.WHITE;
private int arcWidth;
private VerticalFieldManager vfm ;
private EditField editField;
int nn =1;
int count=0;
public BasicField(int width, int height, long style, final String hint,String type_horizontal_vertical)
{
super(style | NO_VERTICAL_SCROLL | NO_HORIZONTAL_SCROLL);
if(type_horizontal_vertical.equalsIgnoreCase("Horizontal"))
vfm = new VerticalFieldManager(HORIZONTAL_SCROLL | USE_ALL_WIDTH );
if(type_horizontal_vertical.equalsIgnoreCase("Vertical"))
vfm = new VerticalFieldManager(VERTICAL_SCROLL| USE_ALL_WIDTH | USE_ALL_HEIGHT);
managerWidth = width;
managerHeight = height;
long innerStyle = style & (READONLY | FOCUSABLE_MASK); // at least
if (innerStyle == 0)
{
innerStyle = FOCUSABLE;
}
if(style==EditField.FILTER_EMAIL)
{
innerStyle = EditField.FILTER_EMAIL;
}
if(style==EditField.FILTER_INTEGER)
{
innerStyle = EditField.FILTER_INTEGER;
}
editField = new EditField("", null, EditField.DEFAULT_MAXCHARS, innerStyle)
{
public void paint(Graphics g)
{
if(editField.getText().length()==0)
{
g.setColor(Color.GRAY);
g.drawText(hint,5,2);
super.paint(g);
}
g.setColor(Color.BLACK);
super.paint(g);
}
};
arcWidth = editField.getFont().getHeight() & 0xFFFFFFFE; // make it even
add(vfm);
vfm.add(editField);
}
public void setFont(Font font)
{
super.setFont(font);
editField.setFont(font);
arcWidth = editField.getFont().getHeight() & 0xFFFFFFFE;
updateLayout();
}
public void setBorderColors(int inactive, int active)
{
inactiveBorderColor = inactive;
activeBorderColor = active;
invalidate();
}
public void setBackgroundColor(int bgColor)
{
backgroundColor = Color.AQUA;
invalidate();
}
public String getText()
{
return editField.getText();
}
public void setText(String newText)
{
editField.setText(newText);
}
public int getPreferredWidth()
{
return managerWidth;
}
public int getPreferredHeight()
{
return managerHeight;
}
protected void sublayout(int w, int h)
{
if (managerWidth == 0)
{
managerWidth = w;
}
if (managerHeight == 0)
{
managerHeight = h;
}
int actWidth = Math.min(managerWidth, w);
int actHeight = Math.min(managerHeight, h);
layoutChild(vfm, actWidth - arcWidth, actHeight - arcWidth);
setPositionChild(vfm, arcWidth / 2, arcWidth / 2);
setExtent(actWidth, actHeight);
}
protected void paint(Graphics g)
{
int prevColor = g.getColor();
int myWidth = managerWidth;
int myHeight = managerHeight;
g.setColor(backgroundColor);
g.fillRoundRect(0, 0, myWidth, myHeight, arcWidth, arcWidth);
g.setColor(borderColor);
g.drawRoundRect(0, 0, myWidth, myHeight, 10,10);
g.drawRoundRect(1, 1, myWidth - 2, myHeight - 2, 10-2, 10-2);
g.setColor(prevColor);
super.paint(g);
}
private void adjustBorderColor()
{
int nextColor;
if (editField.isFocus())
{
nextColor = activeBorderColor;
}
else
{
nextColor = inactiveBorderColor;
}
if (borderColor != nextColor)
{
borderColor = nextColor;
invalidate();
}
}
}