问题描述
我的应用程序有AutoCompleteField持有长文本超过100个字符,如果我用普通AutoCompleteField我不能读取数据的其余部分。
我怎样才能使文本换到2行以上的autocompletefield选择?
我尝试使用'\\ r'+'\\ n'和'\\ n',它没有给新行。设置它的大小,也可以设置行的高度犯规给我我想要的结果。
AutoCompleteField autoCustomer =新AutoCompleteField(CUSTLIST,风格);
autoCustomer.getListField()的setSize(20)。
autoCustomer.getListField()setRowHeight(100)。
如果我是你,我会重写 drawListRow
并使用绘制文本的drawText
,这将给我就行应该如何看的完全控制。尽量使您的code,以这样的表现。
AutoCompleteField autoCompleteField =新AutoCompleteField(
filterList,AutoCompleteField.LIST_STATIC){ 公共无效drawListRow(ListField listField,图形克,
INT指数,诠释Y,int width)将{ BasicFilteredListResult结果=(BasicFilteredListResult)(autoCompleteField
获得(listField,指数));
如果(结果!= NULL)
{
//这里绘制文本
}
} 公共无效ONSELECT(对象选择,整型){
super.onSelect(选择型);
如果(选择!= NULL){ BasicFilteredListResult结果=(BasicFilteredListResult)本
.getSelectedObject();
handleResult((字符串)result._object); }其他{
Dialog.alert(资源
.getString(PLEASE_PICK_A_VALID_NAME));
返回;
}
}
};
如果你想换你的文字,你可以用下面的方法
//方便的方法来包装用特定字体绘制成与行文本
//最大宽度
//这里找到:
// http://supportforums.blackberry.com/t5/Java-Development/Can-drawText-wrap-text-into-multiple-lines/m-p/499901
公共静态的String [] wrapText(字符串文本,字体楼INT了maxWidth){
矢量结果=新的向量();
如果(文字== NULL)
返回新的String [] {}; 布尔hasMore = TRUE; //光标的当前索引
INT电流= 0; //下一行破指数
INT换行符= -1; //线突破后的空间
INT nextSpace = -1; 而(hasMore){
//查找换行符
而(真){
换行符= nextSpace;
如果(LINEBREAK == text.length() - 1){
//我们已经到达最后一行
hasMore = FALSE;
打破;
}
nextSpace = text.indexOf('',换行符+ 1);
如果(nextSpace == -1)
nextSpace = text.length() - 1;
INT线宽= F
.getAdvance(文字,当前,nextSpace - 电流);
//如果时间过长,打破查找循环
如果(线宽>了maxWidth)
打破;
}
串线= text.substring(电流,断行+ 1);
result.addElement(线);
电流=断行+ 1;
}
的String [] = resultArray新的String [result.size()];
result.copyInto(resultArray);
返回resultArray;
}
My apps has AutoCompleteField that hold long text more than 100 Characters, if I use regular AutoCompleteField I cant read the rest of data.
How can I make the text wrap into 2 or more lines in the autocompletefield options ?I try using '\r'+'\n' and '\n', its not giving new line. setting it size and also set row height doesnt give me the result I wanted
AutoCompleteField autoCustomer = new AutoCompleteField(custList, style);
autoCustomer.getListField().setSize(20);
autoCustomer.getListField().setRowHeight(100);
If I was you I would override drawListRow
and draw the text using drawText
which will give me total control on how the row should look. Try adapting your code to behave like this
AutoCompleteField autoCompleteField = new AutoCompleteField(
filterList, AutoCompleteField.LIST_STATIC) {
public void drawListRow(ListField listField, Graphics g,
int index, int y, int width) {
BasicFilteredListResult result = (BasicFilteredListResult) (autoCompleteField
.get(listField, index));
if (result != null)
{
//Draw text here
}
}
public void onSelect(Object selection, int type) {
super.onSelect(selection, type);
if (selection != null) {
BasicFilteredListResult result = (BasicFilteredListResult) this
.getSelectedObject();
handleResult((String) result._object);
} else {
Dialog.alert(Resource
.getString(PLEASE_PICK_A_VALID_NAME));
return;
}
}
};
IF you want to wrap your text you can use the following method
// Handy method to wrap text drawn with the specified font into rows with
// the max width
// Found here:
// http://supportforums.blackberry.com/t5/Java-Development/Can-drawText-wrap-text-into-multiple-lines/m-p/499901
public static String[] wrapText(String text, Font f, int maxWidth) {
Vector result = new Vector();
if (text == null)
return new String[] {};
boolean hasMore = true;
// The current index of the cursor
int current = 0;
// The next line break index
int lineBreak = -1;
// The space after line break
int nextSpace = -1;
while (hasMore) {
// Find the line break
while (true) {
lineBreak = nextSpace;
if (lineBreak == text.length() - 1) {
// We have reached the last line
hasMore = false;
break;
}
nextSpace = text.indexOf(' ', lineBreak + 1);
if (nextSpace == -1)
nextSpace = text.length() - 1;
int linewidth = f
.getAdvance(text, current, nextSpace - current);
// If too long, break out of the find loop
if (linewidth > maxWidth)
break;
}
String line = text.substring(current, lineBreak + 1);
result.addElement(line);
current = lineBreak + 1;
}
String[] resultArray = new String[result.size()];
result.copyInto(resultArray);
return resultArray;
}
这篇关于总结AutoCompleteField列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!