blackberry how to create Tabbar?
Hi all how to create Tab bar in blackberry开发者_Go百科 ?
Take a look at this entry:
How To - Create tabbed view screens
The example included:
package com.rim.samples.tabcontrol;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FocusChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class TabControl extends UiApplication {
public TabControl() {
TabControlScreen screen = new TabControlScreen();
pushScreen(screen);
}
/**
* @param args
*/
public static void main(String[] args) {
TabControl app = new TabControl();
app.enterEventDispatcher();
}
private class TabControlScreen extends MainScreen implements FocusChangeListener {
private LabelField tab1;
private LabelField tab2;
private LabelField tab3;
private LabelField spacer1;
private LabelField spacer2;
private VerticalFieldManager tabArea;
private LabelField tab1Heading;
private BasicEditField tab1Field1;
private BasicEditField tab1Field2;
private LabelField tab2Heading;
private BasicEditField tab2Field1;
private BasicEditField tab2Field2;
private LabelField tab3Heading;
private BasicEditField tab3Field1;
private BasicEditField tab3Field2;
private VerticalFieldManager tab1Manager;
private VerticalFieldManager tab2Manager;
private VerticalFieldManager tab3Manager;
public TabControlScreen() {
HorizontalFieldManager hManager = new HorizontalFieldManager();
tab1 = new LabelField("Page 1", LabelField.FOCUSABLE | LabelField.HIGHLIGHT_SELECT);
tab2 = new LabelField("Page 2", LabelField.FOCUSABLE | LabelField.HIGHLIGHT_SELECT);
tab3 = new LabelField("Page 3", LabelField.FOCUSABLE | LabelField.HIGHLIGHT_SELECT);
spacer1 = new LabelField(" | ", LabelField.NON_FOCUSABLE);
spacer2 = new LabelField(" | ", LabelField.NON_FOCUSABLE);
tab1.setFocusListener(this);
tab2.setFocusListener(this);
tab3.setFocusListener(this);
hManager.add(tab1);
hManager.add(spacer1);
hManager.add(tab2);
hManager.add(spacer2);
hManager.add(tab3);
add(hManager);
add(new SeparatorField());
tab1Manager = new VerticalFieldManager();
tab2Manager = new VerticalFieldManager();
tab3Manager = new VerticalFieldManager();
tabArea = displayTab1();
add(tabArea);
}
public void focusChanged(Field field, int eventType) {
if (tabArea != null) {
if (eventType == FOCUS_GAINED) {
if (field == tab1) {
System.out.println("Switch to Tab 1");
delete(tabArea);
tabArea = displayTab1();
add(tabArea);
} else if (field == tab2) {
System.out.println("Switch to Tab 2");
System.out.println("Switch to Tab 1");
delete(tabArea);
tabArea = displayTab2();
add(tabArea);
} else if (field == tab3) {
System.out.println("Switch to Tab 3");
System.out.println("Switch to Tab 1");
delete(tabArea);
tabArea = displayTab3();
add(tabArea);
}
}
}
}
public VerticalFieldManager displayTab1() {
if (tab1Heading == null) {
tab1Heading = new LabelField("Registration");
tab1Manager.add(tab1Heading);
}
if (tab1Field1 == null) {
tab1Field1 = new BasicEditField("Username: ", "");
tab1Manager.add(tab1Field1);
}
if (tab1Field2 == null) {
tab1Field2 = new BasicEditField("Password: ", "");
tab1Manager.add(tab1Field2);
}
return tab1Manager;
}
public VerticalFieldManager displayTab2() {
if (tab2Heading == null) {
tab2Heading = new LabelField("Password Recovery");
tab2Manager.add(tab2Heading);
}
if (tab2Field1 == null) {
tab2Field1 = new BasicEditField("Security Question: ", "Mother's Maiden Name?");
tab2Manager.add(tab2Field1);
}
if (tab2Field2 == null) {
tab2Field2 = new BasicEditField("Password: ", "");
tab2Manager.add(tab2Field2);
}
return tab2Manager;
}
public VerticalFieldManager displayTab3() {
if (tab3Heading == null) {
tab3Heading = new LabelField("Interests");
tab3Manager.add(tab3Heading);
}
if (tab3Field1 == null) {
tab3Field1 = new BasicEditField("Hobbies: ", "");
tab3Manager.add(tab3Field1);
}
if (tab3Field2 == null) {
tab3Field2 = new BasicEditField("Memberships: ", "");
tab3Manager.add(tab3Field2);
}
return tab3Manager;
}
}
}
for the custom tabbar:
public class BottomPanel extends VerticalFieldManager implements
FieldChangeListener {
Bitmap news_bit = Bitmap.getBitmapResource("news.png");// these images are
// cutting according
// to device
// requirement;
Bitmap news_bit_hover = Bitmap.getBitmapResource("news_h.png");
Bitmap settings_bit = Bitmap.getBitmapResource("settings.png");
Bitmap settings_bit_hover = Bitmap.getBitmapResource("settings_h.png");
Bitmap about_bit = Bitmap.getBitmapResource("about.png");
Bitmap about_bit_hover = Bitmap.getBitmapResource("about_h.png");
PictureBackgroundButtonField news_pic, settings_pic, about_pic;
HorizontalFieldManager hr;
int current_index = 0;
public BottomPanel(int current_index) {
super(FOCUSABLE);
this.current_index = current_index;
VerticalFieldManager ver = new VerticalFieldManager(USE_ALL_WIDTH
| USE_ALL_HEIGHT) {
protected void sublayout(int width, int height) {
super.sublayout(width, news_bit.getHeight());
setExtent(width, news_bit.getHeight());
}
};
hr = new HorizontalFieldManager(FIELD_HCENTER);
if (current_index == 1) {
news_pic = new PictureBackgroundButtonField(news_bit.getWidth(),
news_bit.getHeight(), Field.NON_FOCUSABLE
| Field.FIELD_VCENTER, news_bit_hover,
news_bit_hover);
} else {
news_pic = new PictureBackgroundButtonField(news_bit.getWidth(),
news_bit.getHeight(),
Field.FOCUSABLE | Field.FIELD_VCENTER, news_bit,
news_bit_hover);
}
news_pic.setChangeListener(this);
hr.add(news_pic);
if (current_index == 2) {
settings_pic = new PictureBackgroundButtonField(
settings_bit.getWidth(), settings_bit.getHeight(),
Field.NON_FOCUSABLE | Field.FIELD_VCENTER,
settings_bit_hover, settings_bit_hover);
} else {
settings_pic = new PictureBackgroundButtonField(
settings_bit.getWidth(), settings_bit.getHeight(),
Field.FOCUSABLE | Field.FIELD_VCENTER, settings_bit,
settings_bit_hover);
}
settings_pic.setChangeListener(this);
hr.add(settings_pic);
if (current_index == 3) {
about_pic = new PictureBackgroundButtonField(about_bit.getWidth(),
about_bit.getHeight(), Field.NON_FOCUSABLE
| Field.FIELD_VCENTER, about_bit_hover,
about_bit_hover);
} else {
about_pic = new PictureBackgroundButtonField(about_bit.getWidth(),
about_bit.getHeight(), Field.FOCUSABLE
| Field.FIELD_VCENTER, about_bit, about_bit_hover);
}
about_pic.setChangeListener(this);
hr.add(about_pic);
ver.add(hr);
add(ver);
ver.setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));
}
public void fieldChanged(Field field, int context) {
if (field == news_pic) {
LoadingScreenDemo loadingScreen = new LoadingScreenDemo(1);
UiApplication.getUiApplication().popScreen(
UiApplication.getUiApplication().getActiveScreen());
UiApplication.getUiApplication().pushScreen(loadingScreen);
//loadingScreen.createGUI();
} else if (field == settings_pic) {
SecondTab loadingScreen = new SecondTab(2);
UiApplication.getUiApplication().popScreen(
UiApplication.getUiApplication().getActiveScreen());
UiApplication.getUiApplication().pushScreen(loadingScreen);
} else if (field == about_pic) {
ThirdTab loadingScreen = new ThirdTab(3);
UiApplication.getUiApplication().popScreen(
UiApplication.getUiApplication().getActiveScreen());
UiApplication.getUiApplication().pushScreen(loadingScreen);
}
}
}
public class PictureBackgroundButtonField extends Field {
private String _label;
private int _labelHeight;
private int _labelWidth;
private Font _font;
private Bitmap _currentPicture;
private Bitmap _onPicture;
private Bitmap _offPicture;
public PictureBackgroundButtonField(int width, int height, long style,
Bitmap picture, Bitmap selectedPic) {
super(style);
_font = getFont();
_label = "";
_labelHeight = height;
_labelWidth = width;
_currentPicture = picture;
_onPicture = selectedPic;
_offPicture = picture;
}
public int getPreferredHeight() {
return _labelHeight + 10;
}
public int getPreferredWidth() {
return _labelWidth + 20;
}
protected void onFocus(int direction) {
_currentPicture = _onPicture;
invalidate();
}
protected void onUnfocus() {
_currentPicture = _offPicture;
invalidate();
}
protected void drawFocus(Graphics graphics, boolean on) {
// Do nothing
}
protected void layout(int width, int height) {
setExtent(getPreferredWidth(), getPreferredHeight());
}
protected void paint(Graphics graphics) {
graphics.drawBitmap(3, 6, getPreferredWidth(), getPreferredHeight(),
_currentPicture, 0, 0);
graphics.setFont(_font);
graphics.drawText(
_label,
4,
2,
(int) (getStyle() & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK),
getWidth() - 6);
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return true;
}
}
set as a bottom in class:
BottomPanel bottomPanel=new BottomPanel(0);
setStatus(bottomPanel);
精彩评论