简单的解决办法:
可以设置EditField不能接受回车键,设置最大字符个数。
EditField loginIDField = new EditField("Login", "hello ", MAX_CHARS, EditField.NO_NEWLINE);
但是,如果用户敲入字符小于“最大字符个数”,但是超出EditField的长度的时候,EditField还是会自动换行。
解决办法:
创建一个定制组件,将EditField放到HorizontalFieldManager ,而HorizontalFieldManager 使用HORIZONTAL_SCROLL style可以不换行但是可以录入很多字符,即使超长也不会换行。
/* http://www.dogizmo.com/gadget/sample-code-scrollable-one-line-text-input-field */
package com.rim.samples.device.helloworlddemo;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
public class OneLineTextField extends HorizontalFieldManager {
private EditField _editField;
public OneLineTextField(String label, String initialValue, int maxChars, long style) {
super(HORIZONTAL_SCROLL);
_editField = new EditField(label, initialValue, maxChars, style | EditField.NO_NEWLINE | EditField.FOCUSABLE | EditField.EDITABLE);
add(_editField);
}
/*
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(getPreferredWidth(), maxHeight );
}
public int getPreferredWidth()
{
return 200;
}
*/
public String getText() {
return _editField.getText();
}
}