问题
I want a non-editable TextField (or a subclass) that doesn't even have the caret displayed. Alternatively, I want a multiline LabelField. Is any of these possible?
回答1:
TextField without focus cursor
TextField readOnly = new TextField(NON_FOCUSABLE);
readOnly.setText("Read only, no carret");
add(readOnly);
TextField drawFocus override
If text is too large to fit the screen, you can override drawFocus method in TextField, so scrolling will be available:
TextField readOnly = new TextField(READONLY)
{
protected void drawFocus(Graphics graphics, boolean on) {}
};
TextFields, separated with NullFields
Other option is to split TextField into several, separated with NullFields:
class Scr extends MainScreen {
public Scr() {
String text = "Lorem ipsum dolor sit amet, consectetuer "
+ "adipiscing elit, sed diam nonummy nibh euismod "
+ "tincidunt ut laoreet dolore magna aliquam erat "
+ "volutpat. Ut wisi enim ad minim veniam, quis "
+ "nostrud exerci tation ullamcorper suscipit "
+ "lobortis nisl ut aliquip ex ea commodo consequat. "
+ "Duis autem vel eum iriure dolor in hendrerit in "
+ "vulputate velit esse molestie consequat, vel "
+ "illum dolore eu feugiat nulla facilisis at vero "
+ "eros et accumsan et iusto odio dignissim qui "
+ "blandit praesent luptatum zzril delenit augue "
+ "duis dolore te feugait nulla facilisi.";
text = addScrollText(text, 150);
}
private String addScrollText(String text, int partSize) {
while (0 < text.length()) {
int len = Math.min(partSize, text.length());
TextField readOnly = new TextField(NON_FOCUSABLE);
readOnly.setText(text.substring(0, len));
add(readOnly);
add(new NullField());
text = text.substring(len);
}
return text;
}
}
Multiline LabelField
Multiline text in LabelField, just use newline escape character:
String text = "first line \nnew line \nanother line";
LabelField multiLine = new LabelField(text);
add(multiLine);
回答2:
Yes, you can override the paint method of each UI Element
An example:
public class WhiteLabelField extends LabelField
{
public WhiteLabelField()
{
super();
}
public WhiteLabelField(ObjectGroup text)
{
super(text);
}
public WhiteLabelField(Object text, long style)
{
super(text, style);
}
public void paint(Graphics _g)
{
_g.setColor(Color.WHITE);
super.paint(_g);
}
// Custom
public void setSmallFontSize()
{
setFont( Font.getDefault( ).derive( Font.PLAIN, 16 ));
}
}
回答3:
You could also use a RichTextField
with a style of Field.NON_FOCUSABLE
and you will have what you want.
来源:https://stackoverflow.com/questions/1417225/blackberry-java-textfield-without-the-caret