我们如何在RichTextField中设置背景和字体颜色?我试图覆盖paint()方法,除了这里描述的内容之外,但是当我向下滚动时,背景会被擦除或重置为白色背景。
class ExRichTextField extends RichTextField {
int mTextColor;
public ExRichTextField(String text, int bgColor, int textColor) {
super(text);
mTextColor = textColor;
Background background = BackgroundFactory
.createSolidBackground(bgColor);
setBackground(background);
}
protected void paint(Graphics graphics) {
graphics.setColor(mTextColor);
super.paint(graphics);
}
}
对于RIM 4.5及以下版本,使用paint事件来自己绘制背景:
class ExRichTextField extends RichTextField {
int mTextColor;
int mBgColor;
public ExRichTextField(String text, int bgColor, int textColor) {
super(text);
mTextColor = textColor;
mBgColor = bgColor;
}
protected void paint(Graphics graphics) {
graphics.clear();
graphics.setColor(mBgColor);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.setColor(mTextColor);
super.paint(graphics);
}
}
RichTextField mes_=new RichTextField("texto de ejemplo",Field.NON_FOCUSABLE){
protected void paint(Graphics g){
g.setColor(0x00e52f64);
super.paint(g);
}
};
mes_.setBackground(BackgroundFactory.createSolidBackground(0xFFFADDDA));
声明中嵌入的方法是用于更改字体颜色的。创建后调用的方法是用于将背景更改为纯色。