我正在一个 BlackBerry 应用程序中工作,我需要在其中使用底部的选项卡。
早些时候,我已经使用了选项卡,但位于屏幕顶部。要将选项卡放在屏幕底部,我使用了该sublayout()
方法。但是这样做的副作用是我现在不能使用该display(index)
方法。每次当我单击任何选项卡时,只会选择第一个选项卡。当我隐藏sublayout()
方法时,一切正常。
这是我正在使用的代码:
TabControl.java
public class TabControl extends MainScreen{
public static int tabHeight = 0;
public static TabField mTabField = null;
public static Bitmap BACKGROUND_IMAGE = Bitmap
.getBitmapResource("Background_Superhero.png");
private UiApplication uiApp = UiApplication.getUiApplication();
private UiEngine ui = Ui.getUiEngine();
private int displayHieght;
public TabControl(BuddyListField buddyList) {
super(MainScreen.NO_VERTICAL_SCROLL);
}
public void addFields() {
try {
mTabField = new TabField(this);
add(mTabField);
HomeTab home = new HomeTab();
mTabField.addTab(Bitmap.getBitmapResource("home_tab.png"), home,
Field.FOCUSABLE);
SettingTab setting = new SettingTab();
mTabField.addTab(Bitmap.getBitmapResource("Setting_tab.png"), setting,
Field.FOCUSABLE);
AboutTab about = new AboutTab();
mTabField.addTab(Bitmap.getBitmapResource("abouttab.png"), about,
Field.FOCUSABLE);
mTabField.setDefault(Constant.SETTING_TAB_INDEX);
} catch (Exception e) {
System.out.println(e+"=-====>>TabControl");
e.printStackTrace();
}
}
protected boolean keyDown(int keycode, int time) {
if (keycode == 1769472) {
// escape pressed
return true;
}
return super.keyDown(keycode, time);
}
}
TabField.java
public class TabField extends VerticalFieldManager {
private static Bitmap tabBackGroundImage = Bitmap
.getBitmapResource("tab_bar.png");
private Background CONTROL_NORMAL_BG = BackgroundFactory
.createBitmapBackground(tabBackGroundImage);
private Background CONTROL_ACTIVE_BG = BackgroundFactory
.createSolidBackground(Color.SILVER);
public static int indexValue = 0;
private static int mCurrentIndex = 0;
private int mDefault = 0;
private Field mCurrentField = null;
private Vector mTabFields = new Vector();
private MainScreen _mainscreen = null;
private HorizontalFieldManager mTabController = new HorizontalFieldManager();
private UiApplication uiApp = UiApplication.getUiApplication();
private UiEngine ui = Ui.getUiEngine();
private int tabHieght;
private int displayHieght;
public TabField(MainScreen mainscreen) {
super(MainScreen.USE_ALL_HEIGHT);
_mainscreen = mainscreen;
displayHieght = Display.getHeight();
add(mTabController);
SeparatorField separatorField = new SeparatorField();
separatorField.setBackground(CONTROL_ACTIVE_BG);
add(separatorField);
}
public void setDefault(int index) {
mDefault = index;
if (mDefault <= mTabFields.size() - 1) {
display(mDefault);
}
}
public void display(int index) {
VirtualKeyboard virtKbd = _mainscreen.getVirtualKeyboard();
if(virtKbd != null)
virtKbd.setVisibility(VirtualKeyboard.RESTORE);
try {
if (mCurrentField != null) {
if (mCurrentField instanceof TabFieldItem) {
((TabFieldItem) mCurrentField).onUnSelected();
}
delete(mCurrentField);
}
mCurrentField = (Field) mTabFields.elementAt(index);
add(mCurrentField);
mCurrentField.setFocus();
if (mCurrentField instanceof TabFieldItem) {
((TabFieldItem) mCurrentField).onSelected();
}
setDirty(false);
BitmapField mCurrentBG = (BitmapField) mTabController
.getField(mCurrentIndex);
mCurrentBG.setBackground(CONTROL_NORMAL_BG);
mCurrentBG.setBitmap(getOnUnFocusImg(mCurrentIndex));
BitmapField mBG = (BitmapField) mTabController.getField(index);
mBG.setBackground(CONTROL_ACTIVE_BG);
mBG.setBitmap(getOnFocusImg(index));
mCurrentIndex = index;
if(virtKbd != null)
virtKbd.setVisibility(VirtualKeyboard.HIDE);
if (indexValue == 3) {
}
} catch (Exception e) {
System.out.println("Exception: In TabField--->display() "
+ e.toString());
}
}
public void addTab(Bitmap aBitmap, final Field aTabField, long style) {
BitmapField lButton = null;
if (style == Field.FOCUSABLE) {
final BitmapField focusbleButton = new BitmapField(aBitmap,
Field.FOCUSABLE) {
protected boolean navigationClick(int status, int time) {
if (aTabField == null) {
return false;
}
indexValue = getIndex();
display(indexValue);
return true;
}
protected void paint(Graphics graphics) {
if (indexValue == getIndex()) {
graphics.setBackgroundColor(Color.SILVER);
graphics.clear();
}
super.paint(graphics);
}
public void setSpace(int hSpace, int vSpace) {
super.setSpace(hSpace, vSpace);
}
};
focusbleButton.setBackground(CONTROL_NORMAL_BG);
setlButtonSpace(focusbleButton);
lButton = focusbleButton;
} else {
lButton = new BitmapField(aBitmap);
}
/*if (WallpaperMainScreen.tabHeight == 0)
WallpaperMainScreen.tabHeight = lButton.getBitmapHeight();*/
mTabController.add(lButton);
mTabFields.addElement(aTabField);
if (mDefault == mTabFields.size() - 1 && aTabField != null) {
display(mDefault);
}
}
//SUBLAYOUT METHOD USED TO BRING THE TABBAR AT THE BOTTOM OF THE SCREEN---------------
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(maxWidth, maxHeight);
tabHieght = mTabController.getHeight();
int y = displayHieght - tabHieght;
setPositionChild(mTabController, 0, y);
}
private void setlButtonSpace(BitmapField lButton) {
int tabWidth = Display.getWidth() / 3;
int imagewidth = lButton.getBitmapWidth();
int imageheight = lButton.getBitmapHeight();
int hPaddingValue = (tabWidth - imagewidth) / 2;
int vPaddingValue = (tabWidth - imageheight) / 7;
lButton.setSpace(hPaddingValue + 1, vPaddingValue);
}
private Bitmap getOnFocusImg(int index) {
Bitmap image = null;
switch (index) {
case 0:
image = Bitmap.getBitmapResource("home_tab.png");
break;
case 1:
image = Bitmap.getBitmapResource("Setting_tab.png");
break;
case 2:
image = Bitmap.getBitmapResource("about_tab.png");
break;
/*case 3:
image = Bitmap.getBitmapResource("exit_tab.png");
break;
*/
}
return image;
}
private Bitmap getOnUnFocusImg(int currentIndex) {
Bitmap image = null;
switch (currentIndex) {
case 0:
image = Bitmap.getBitmapResource("home_tab.png");
break;
case 1:
image = Bitmap.getBitmapResource("Setting_tab.png");
break;
case 2:
image = Bitmap.getBitmapResource("about_tab.png");
break;
/*case 3:
image = Bitmap.getBitmapResource("exit_tab.png");
break;*/
}
return image;
}
protected boolean keyChar(char ch, int status, int time) {
if (mCurrentField != null && mCurrentField instanceof TabFieldItem) {
return ((TabFieldItem) mCurrentField).keyChar(ch, status, time);
} else {
return super.keyChar(ch, status, time);
}
}
}
TabFieldItem.java
package com.np.custom;
public interface TabFieldItem {
/**
* Method invoked when the tabField is about to be displayed
*/
public void onSelected();
/**
* Method invoked when the tabField is about to be removed from the display
*/
public void onUnSelected();
public boolean keyChar(char ch, int status, int time);
}
我在 TabField.java 类中使用了 sublyout 方法。结果如图所示。