изменение цвета фона Blackberry ButtonField - PullRequest
0 голосов
/ 09 декабря 2011

Я хотел изменить цвет фона ButtonField в черной ягоде jde 6, переопределив метод рисования, но не может изменить весь фон ButtonField

Ответы [ 3 ]

4 голосов
/ 14 декабря 2011

Я решил эту проблему, расширив класс 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 голос
/ 09 декабря 2011

попробуйте добавить

protected void applyTheme()
{
}

Удалит границы / темы

0 голосов
/ 09 декабря 2011

Этот код помогает вам:

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) является границей для этой кнопки.

...