我想通过覆盖改变黑莓jde 6中ButtonField的背景颜色 paint方法,但无法更改ButtonField的整个背景
答案 0 :(得分:4)
我通过扩展Field
类解决了这个问题。这是我的版本。希望它有所帮助。
/**
* ColorButtonField is a custom button field that creates buttons of a specified
* size and color.
*/
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.Keypad;
public class ColorButtonField extends Field
{
private int backgroundColour;
private int highlightColour;
private int colour;
private int fieldWidth;
private int fieldHeight;
private String text;
private int padding = 8;
private double fontWidth;
private Font font;
public ColorButtonField(String text, int highlightColour, int backgroundColour)
{
super();
this.text = text;
this.highlightColour = highlightColour;
this.backgroundColour = backgroundColour;
colour = backgroundColour;
font = Font.getDefault();
fieldHeight = font.getHeight() + padding;
fontWidth = font.getHeight() / 2;
fieldWidth = (Display.getWidth() - 12) / 3;
this.setPadding(2, 2, 2, 2);
}
public ColorButtonField(String text, int highlightColour, int backgroundColour, int width, int height, int padding)
{
super();
this.text = text;
this.highlightColour = highlightColour;
this.backgroundColour = backgroundColour;
colour = backgroundColour;
font = Font.getDefault();
fieldHeight = height;
fieldWidth = width;
this.setPadding(2, padding, 2, padding);
}
public void setFont(Font font){
this.font = font;
}
public void setFocus(){
super.setFocus();
}
public boolean isFocusable(){
return true;
}
protected boolean keyChar(char character, int status, int time) {
if (character == Keypad.KEY_ENTER) {
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}
protected boolean navigationClick(int status, int time)
{
fieldChangeNotify(0);
return true;
}
protected void onFocus(int direction)
{
colour = highlightColour;
invalidate();
}
protected void onUnfocus()
{
colour = backgroundColour;
invalidate();
}
public int getPreferredWidth()
{
return fieldWidth;
}
public int getPreferredHeight()
{
return fieldHeight;
}
protected void layout(int arg0, int arg1)
{
setExtent(getPreferredWidth(), getPreferredHeight());
}
protected void drawFocus(Graphics graphics, boolean on)
{
}
protected void fieldChangeNotify(int context)
{
try
{
this.getChangeListener().fieldChanged(this, context);
}
catch (Exception e)
{}
}
protected void paint(Graphics graphics)
{
graphics.setColor(colour);
graphics.fillRoundRect(0, 0, fieldWidth, fieldHeight, 8, 8);
graphics.setColor(colour + 0x333333);
graphics.drawRoundRect(0, 0, fieldWidth, fieldHeight, 8, 8);
graphics.setColor(Color.WHITE);
if( 0 < text.indexOf(' ')){
graphics.drawText(text.substring(0, text.indexOf(' ') ), fieldWidth / 2 - font.getAdvance(text.substring(0, text.indexOf(' ') )) / 2 , 5);
graphics.drawText(text.substring(text.indexOf(' ') + 1), fieldWidth / 2 - font.getAdvance(text.substring(text.indexOf(' ') + 1)) / 2 , fieldHeight / 2);
}
else
graphics.drawText(text, fieldWidth / 2 - font.getAdvance(text) / 2 , fieldHeight / 2 - 10);
}
}
答案 1 :(得分:1)
尝试添加
protected void applyTheme()
{
}
它将删除边框/主题
答案 2 :(得分:0)
此代码可以帮助您:
ButtonField save=new ButtonField("Save",FIELD_HCENTER);
save.setBackground(BackgroundFactory.createSolidBackground(Color.GREEN));
Border border=BorderFactory.createRoundedBorder(new XYEdges(2,2,2,2));
save.setBorder(border);
add(save);
此处 XYEdges(2,2,2,2)是该按钮的边框。