Hello everyone开发者_运维知识库 I am new to blackberry and I want a textfield that scroll it's text i.e greater than the preferred width horizontally , also able to show label out side the text draw area ( e.g. on the left side). Please help me.
This may be achieved by combining non-scrolling and scrolling HorizontalFieldManagers.
Try this code:
class Scr extends MainScreen {
public Scr() {
HorizontalFieldManager fieldHolder = new HorizontalFieldManager(
NO_HORIZONTAL_SCROLL | NO_HORIZONTAL_SCROLLBAR);
fieldHolder.add(new LabelField("some label: "));
HorizontalFieldManager editHolder = new HorizontalFieldManager(
HORIZONTAL_SCROLL | HORIZONTAL_SCROLLBAR);
editHolder.add(new TextField(TextField.NO_NEWLINE));
fieldHolder.add(editHolder);
add(fieldHolder);
}
}
Setting default text code:
class Scr extends MainScreen {
public Scr() {
HorizontalFieldManager fieldHolder = new HorizontalFieldManager(
NO_HORIZONTAL_SCROLL | NO_HORIZONTAL_SCROLLBAR);
fieldHolder.add(new LabelField("some label: "));
HorizontalFieldManager editHolder = new HorizontalFieldManager(
HORIZONTAL_SCROLL | HORIZONTAL_SCROLLBAR);
TextField textField = new TextField(TextField.NO_NEWLINE);
editHolder.add(textField);
fieldHolder.add(editHolder);
add(fieldHolder);
// set some text then
String text = "Lorem ipsum dolor sit amet, consectetuer"+
" adipiscing elit, sed diam nonummy nibh euismod tincidunt"+
" ut laoreet dolore magna aliquam erat volutpat.";
textField.setText(text);
}
}
And something which basically works on 4.6/4.7:
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.";
HorizontalFieldManager fieldHolder = new HorizontalFieldManager(
NO_HORIZONTAL_SCROLL | NO_HORIZONTAL_SCROLLBAR);
fieldHolder.add(new LabelField("some label: "));
HorizontalFieldManager editHolder = new HorizontalFieldManager(
HORIZONTAL_SCROLL | HORIZONTAL_SCROLLBAR);
TextField textField = new TextField("",text,1024,TextField.NO_NEWLINE);
editHolder.add(textField);
fieldHolder.add(editHolder);
add(fieldHolder);
}
}
Border for Manager
Border border = BorderFactory.createSimpleBorder(new XYEdges(4,4,4,4));
fieldHolder.setBorder(border);
Fixed size Manager
class SizedHFM extends HorizontalFieldManager {
int mWidth = 0;
public SizedHFM(int width) {
super(NO_HORIZONTAL_SCROLL | NO_HORIZONTAL_SCROLLBAR);
mWidth = width;
}
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(mWidth, maxHeight);
setExtent(mWidth, getPreferredHeight());
}
}
Sample of use:
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.";
SizedHFM fieldHolder = new SizedHFM(200);
Border border = BorderFactory
.createSimpleBorder(new XYEdges(4, 4, 4, 4));
fieldHolder.setBorder(border);
fieldHolder.add(new LabelField("some label: "));
HorizontalFieldManager editHolder = new HorizontalFieldManager(
HORIZONTAL_SCROLL | HORIZONTAL_SCROLLBAR);
TextField textField = new TextField("", text, 1024,
TextField.NO_NEWLINE);
editHolder.add(textField);
fieldHolder.add(editHolder);
add(fieldHolder);
}
}
精彩评论