I have a requirement for a one-line edit field that can accept text longer than the edit field's width (like a "normal" Windows textbox).
Since the BasicEditField control normally word-wraps its contents, I've implemented this requirement by placing my BasicEditField inside a HorizontalFieldManager that allows horizontal scrolling.
This works in one sense: if I type text longer than the edit开发者_JS百科 field's width, the HFM automatically scrolls to the left (taking the edit field with it) which keeps the cursor visible at the right.
The problem is that the user can swipe right-to-left, which moves the BasicEditField completely off the screen to the left. I want to be able to only scroll left when the BasicEditField's contents are too wide to be shown entirely, and only allow just enough leftward scrolling to keep the cursor visible on the right.
I fought this exact scenario when implementing my own EditField. What's happening is since you allow horizontal scrolling, and an EditField by nature wants to use as much width as it can, you end up with a very wide EditField. I'm copying this from my code, so hopefully it will work for you, but if it doesn't add a comment and I'll try to see if I can help get it working.
EditField edit = new EditField(EditField.NO_NEWLINE | style) {
protected void fieldChangeNotify(int context) {
try{
super.fieldChangeNotify(context);
setExtent(this.getFont().getAdvance(this.getText()) + 10, this.getHeight());
}
catch(Exception e){
//don't recall why I needed this, but it works so I'm hesitant to remove it
}
}
protected void layout(int width, int height) {
super.layout(width, height);
setExtent(this.getFont().getAdvance(this.getText()) + 10, this.getHeight());
}
};
What this will do is progressively make the EditField wider as the text grows. I had this inside of an HFM that grew with it as well, but you might not need that. I did this because if the field grew wider than the screen width, rather than scrolling the entire screen you could let this single Field scroll horizontally. Also, the + 10 is in there because it was clipping the cursor without it. If you would like that as well I can edit this post to include it.
精彩评论