在这里,我正在创建一个包含顶部栏、自定义垂直滚动管理器、Editfield
发送消息和发送按钮的聊天屏幕。
现在我创建一个消息布局:它有VerticalFieldManager
并且它包含EditField
可聚焦和只读的,以及两个LabelField
用于名称和日期的 s。此垂直字段管理器是根据聊天消息的数量动态创建的。这一切都在自定义滚动中列出。
我需要滚动才能从屏幕底部开始。现在,它在屏幕顶部工作。
在屏幕底部,我有一个BasicEditField
发送按钮。
每 7 秒刷新一次页面,刷新页面时,我重新创建整个布局。BasicEditField
因此,每当我的页面刷新时,我都需要关注。而且我还希望滚动反向工作。
这是我的垂直滚动管理器类
public class VerticalScrollManager extends VerticalFieldManager implements
ScrollChangeListener {
public VerticalScrollManager() {
this(VERTICAL_SCROLL | NO_VERTICAL_SCROLLBAR|DOWNWARD, Integer.MAX_VALUE, Integer.MAX_VALUE);
}
public VerticalScrollManager(int w, int h) {
this(VERTICAL_SCROLL | NO_VERTICAL_SCROLLBAR|DOWNWARD, w, h);
}
public VerticalScrollManager(long style) {
this(style, Integer.MAX_VALUE, Integer.MAX_VALUE);
}
public VerticalScrollManager(long style, int w, int h) {
super(style);
maxVisibleHeight = h;
maxVisibleWidth = w;
setScrollListener(this);
}
protected void sublayout(int w, int h) {
isScrolling = ((getStyle() & VERTICAL_SCROLL) == VERTICAL_SCROLL);
int scrollbarWidth = isScrolling ? SCROLLBAR_WIDTH + SCROLLBAR_LEFT_MARGIN + SCROLLBAR_RIGHT_MARGIN : 0;
visibleHeight = Math.min(h, maxVisibleHeight);
visibleWidth = Math.min(w, maxVisibleWidth);
int myWidth = visibleWidth - scrollbarWidth;
super.sublayout(myWidth, visibleHeight);
visibleHeight = getHeight();
totalHeight = getVirtualHeight();
visibleWidth = getWidth() + scrollbarWidth;
setExtent(visibleWidth, visibleHeight);
setVirtualExtent(visibleWidth, totalHeight);
isScrolling = (visibleHeight < totalHeight);
if (isScrolling) {
sliderHeight = visibleHeight * visibleHeight / totalHeight;
sliderHeight = Math.max(sliderHeight, 1); // show at least one pixel!
sliderXPosition = visibleWidth - SCROLLBAR_WIDTH - SCROLLBAR_RIGHT_MARGIN;
}
}
public void scrollChanged(Manager mgr, int newX, int newY) {
if (mgr == this) {
invalidate(newX + sliderXPosition, newY, SCROLLBAR_WIDTH + SCROLLBAR_RIGHT_MARGIN, getVisibleHeight());
}
}
}
这是我处理用户界面和功能的屏幕
public class ChatScreen extends MainScreen implements FieldChangeListener, FocusChangeListener {
class MainTask implements Runnable
{
public MainTask()
{}
public void run() {
try
{
if(textMsg.getText().equals(""))
{
IssueHTTPPostL();
}
else
{
IssueHTTPPostChat(textMsg.getText());
}
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
if(replyCode.equalsIgnoreCase("success"))
{
if(chatMsgData!=null && chatMsgData.length>0 )
{
for( int i=0; i<chatMsgData.length; i++)
{
final String name = chatMsgData[i].name;
final String email = chatMsgData[i].email;
final String date = chatMsgData[i].date;
final String msg = chatMsgData[i].message;
final String vappUser = chatMsgData[i].vappUser;
if(chatMsgData[i].isReply.equals("0"))
{
vfm = new VerticalFieldManager(VerticalFieldManager.FIELD_RIGHT)
{
protected void paintBackground(Graphics g)
{
g.setColor(Color.WHITE);
g.fillRoundRect(30, 0, getWidth()-30, getHeight(), 12, 12);
g.setColor(0X626262);
g.drawRoundRect(30, 0, getWidth()-30, getHeight(), 12, 12);
//g.drawBitmap(0,0,getWidth(),getHeight(),imgRight,0,0);
}
};
lblName = new LabelField(name, LabelField.FIELD_LEFT|DrawStyle.LEFT)
{
protected void paint(Graphics g)
{
g.setColor(Color.BLACK);
super.paint(g);
};
};
if(name.equals("")||name.equals("Name"))
{
lblName.setText(email);
}
lblDate = new LabelField(date, LabelField.FIELD_RIGHT|DrawStyle.RIGHT);
lblDate.setFont(font.derive(Font.PLAIN, dateFontSize));
lblName.setFont(font.derive(Font.BOLD, nameFontSize));
JustifiedHorizontalFieldManager hfm = new JustifiedHorizontalFieldManager(lblName, lblDate, true);
hfm.setMargin(10, 10, 10, 50);
lblMsg = new EditField(EditField.FOCUSABLE|EditField.READONLY)
{
protected void paint(Graphics g)
{
g.setColor(0x626262);
super.paint(g);
};
};
lblMsg.setText(msg);
lblMsg.setMargin(10, 10, 10, 50);
lblMsg.setFont(font.derive(Font.PLAIN, msgFontSize));
vfm.add(hfm);
vfm.add(lblMsg);
vfm.setMargin(10, 20, 10, 0);
}
else
{
vfm = new VerticalFieldManager(VerticalFieldManager.FIELD_LEFT)
{
protected void paintBackground(Graphics g)
{
g.setColor(Color.WHITE);
g.fillRoundRect(0, 0, getWidth()-30, getHeight(), 12, 12);
g.setColor(Color.BLACK);
g.drawRoundRect(0, 0, getWidth()-30, getHeight(), 12, 12);
//g.drawBitmap(0,0,getWidth(),getHeight(),imgLeft,0,0);
}
};
lblName = new LabelField(vappUser, LabelField.FIELD_LEFT|DrawStyle.LEFT)
{
protected void paint(Graphics g)
{
g.setColor(Color.BLACK);
super.paint(g);
};
};
if(name.equals("")||name.equals("Name"))
{
lblName.setText(email);
}
lblDate = new LabelField(date, LabelField.FIELD_RIGHT|DrawStyle.RIGHT)
{
protected void paint(Graphics g)
{
g.setColor(0X626262);
super.paint(g);
};
};
lblDate.setFont(font.derive(Font.PLAIN, dateFontSize));
lblName.setFont(font.derive(Font.BOLD, nameFontSize));
JustifiedHorizontalFieldManager hfm = new JustifiedHorizontalFieldManager(lblName, lblDate, true);
//hfm.setMargin("top", "right", "bottom", "left");
hfm.setMargin(10, 50, 10, 10);
lblMsg = new EditField(EditField.FOCUSABLE|EditField.READONLY)
{
protected void paint(Graphics g)
{
g.setColor(0x626262);
super.paint(g);
};
};
lblMsg.setText(msg);
lblMsg.setMargin(10, 40, 10, 10);
lblMsg.setFont(font.derive(Font.PLAIN, msgFontSize));
vfm.add(hfm);
vfm.add(lblMsg);
vfm.setMargin(10, 10, 20, 0);
}
vsm.add(vfm);
}
}
add(vsm);
add(sendHfm);
}
else
{
timer.cancel();
UiApplication.getUiApplication().popScreen(ChatScreen.this);
UiApplication.getUiApplication().pushScreen(new ChatListScreen());
}
}
});
}
catch(Exception e)
{e.printStackTrace();}
}
}
}