我正在使用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方法。 结果如图所示。
答案 0 :(得分:1)
尝试使用setStatus(mTabField)而不是覆盖子布局。
答案 1 :(得分:1)
我有一些建议。
首先,在构建这样的东西时,尤其是对于BlackBerry,可能很难获得所有点击处理或焦点处理正确。从RIM的一个例子开始,你知道有效,这通常是一个好主意。在这种情况下,您可以see their Tab Bar example here。
如果你想坚持你的代码,我看到的一些事情可能是问题:
//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);
}
当您实施sublayout()
时,通常应该为setPositionChild()
所拥有的每个子字段调用layoutChild()
和Manager
。您只是致电setPositionChild()
。通常,调用setExtent()
也是合适的。
我最近看到一些问题,人们覆盖VerticalFieldManager
(或HorizontalFieldManager
),然后仍覆盖sublayout()
以提供自定义布局管理。这首先违背了使用VerticalFieldManager
的目的,因为它为您执行垂直布局,按照add()
顺序布置子字段。如果您想要进行任何自定义,我建议您延长Manager
,而不是VerticalFieldManager
。如果默认的VerticalFieldManager
布局不是您想要的,那么只需提供Manager
所需的完整实现。这不是很困难。只需实施sublayout()
,getPreferredWidth()
和getPreferredHeight()
即可。
当然,您仍然需要正确实施sublayout()
(请参阅上面的 1。)。
无论如何,发布类似问题的其他人在将他们的班级(TabField
改为你)更改为extends Manager
之前也会看到奇怪的行为。这就是我首先尝试解决的问题。
答案 2 :(得分:1)
在'add(mTabController)'上方添加一个管理器,并将该管理器的固定高度覆盖子布局&amp;在sublayout中设置setextent,height必须是displayheight-mtabcontroller高度。并删除tabfield类中重写的子布局。
答案 3 :(得分:0)
对于标签栏示例,在Blackberry JDE(4.5及以上版本)中创建新项目
只需复制下面的代码并执行它。
MyTabAppDemo.java
package com.app;
import net.rim.device.api.ui.UiApplication;
public class MyTabAppDemo extends UiApplication
{ public static void main(String[] args)
{ MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}
public MyApp()
{
pushScreen(new TabScreenExample());
}
}
<强> TabScreenExample.java 强>
package com.app;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class TabScreenExample extends MainScreen {
private VerticalFieldManager mainManager;
private NavigationMenuBar tabBar;
public TabScreenExample() {
tabBar = new NavigationMenuBar() {
public void onNavigationClick(int selection) {
if (NavigationMenuBar.TAB1 == selection) {
// Push Screen Which is releted to first tab
synchronized (Application.getEventLock()) {
UiApplication.getUiApplication().pushScreen(new Screan1());
}
} else if (NavigationMenuBar.TAB2 == selection) {
// Push Screen Which is releted to second tab
synchronized (Application.getEventLock()) {
UiApplication.getUiApplication().pushScreen(new Screan2());
}
}
}
};
mainManager = new VerticalFieldManager() {
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(maxWidth, maxHeight);
setExtent(getPreferredWidth(), getPreferredHeight());
}
public int getPreferredWidth() {
return Display.getWidth();
}
public int getPreferredHeight() {
return Display.getHeight() - tabBar.getPreferredHeight();
}
};
add(mainManager);
add(tabBar);
}
}
//这是自定义标签栏..在此标签栏中,您可以添加许多标签。
的 NavigationMenuBar.java 强>
package com.app;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.KeypadUtil;
import net.rim.device.api.ui.container.HorizontalFieldManager;
public abstract class NavigationMenuBar extends HorizontalFieldManager {
public static final int TAB1 = 0;
public static final int TAB2 = 1;
// Create object of tabs
private TopBarComponenet tab1;
private TopBarComponenet tab2;
// Selected Tab;
private int currentState;
// tab Normal Image
private Bitmap imgTab1Normal, imgTab2Normal;
// tab Focus Image
private Bitmap imgTab1Focus, imgTab2Focus;
public NavigationMenuBar() {
// Normal Images
imgTab1Normal = Bitmap.getBitmapResource("tab1Normal.png");
imgTab2Normal = Bitmap.getBitmapResource("tab2Normal.png");
// Focus Images
imgTab1Focus = Bitmap.getBitmapResource("tab1Focus.png");
imgTab2Focus = Bitmap.getBitmapResource("tab2Focus.png");
// create first tab
tab1 = new TopBarComponenet(imgTab1Normal) {
protected void onClick() {
currentState = TAB1;
selectTab(currentState);
}
};
// create Second tab
tab2 = new TopBarComponenet(imgTab2Normal) {
protected void onClick() {
currentState = TAB2;
selectTab(currentState);
}
};
// add tab button in horizontal manager.
add(tab1);
add(tab2);
}
public int getPreferredHeight() {
return imgTab1Normal.getHeight();
}
public void selectTab(int tab) {
synchronized (Application.getEventLock()) {
// Set Tab visible GUI
if (tab == TAB1) {
tab1.setFocus();
tab1.setBgImage(imgTab1Focus);
tab2.setBgImage(imgTab2Normal);
tab1.selected = true;
tab2.selected = false;
} else if (tab == TAB2) {
tab2.setFocus();
tab1.setBgImage(imgTab1Normal);
tab2.setBgImage(imgTab2Focus);
tab1.selected = false;
tab2.selected = true;
}
}
currentState = tab;
onNavigationClick(currentState);
}
public void deselectAllTab() {
tab1.setBgImage(imgTab1Normal);
tab2.setBgImage(imgTab2Normal);
tab1.selected = false;
tab2.selected = false;
}
public abstract void onNavigationClick(int selection);
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(maxWidth, maxHeight);
setExtent(Display.getWidth(), getPreferredHeight());
}
protected void paintBackground(Graphics gr) {
// TODO Auto-generated method stub
super.paintBackground(gr);
gr.setColor(0x000000);
gr.fillRect(0, 0, Display.getWidth(), getPreferredHeight());
}
private abstract class TopBarComponenet extends Field {
private Bitmap bgImage;
private String text;
public boolean selected;
private int width;
private int height;
public TopBarComponenet(final Bitmap bgImage) {
super(FOCUSABLE);
this.bgImage = bgImage;
// this width depends on how many tab added.
// for example if we want to add 5 tab then
// width = Display.getWidth() / 5;
width = Display.getWidth() / 2;
height = bgImage.getHeight();
}
public void setBgImage(Bitmap image) {
this.bgImage = image;
}
protected void drawFocus(Graphics graphics, boolean on) {
}
protected void layout(int width, int height) {
setExtent(getPreferredWidth(), getPreferredHeight());
}
public int getPreferredHeight() {
return height;
}
public int getPreferredWidth() {
return width;
}
protected void paint(Graphics graphics) {
graphics.drawBitmap((width - bgImage.getWidth()) / 2, 0,
bgImage.getWidth(), bgImage.getHeight(), bgImage, 0, 0);
if (isFocus()) {
graphics.setColor(0xffffff); // white
graphics.setGlobalAlpha(50);
graphics.fillRoundRect(0, 2, width, height - 4, 10, 10);
graphics.setGlobalAlpha(255);
}
}
protected void onFocus(int direction) {
super.onFocus(direction);
invalidate();
}
protected void onUnfocus() {
super.onUnfocus();
invalidate();
}
protected boolean keyDown(int keycode, int time) {
char c = KeypadUtil.getKeyChar(keycode,
KeypadUtil.MODE_UI_CURRENT_LOCALE);
if (c == '\n') {
onClick();
} else if (keycode == 13) {
onClick();
}
return super.keyDown(keycode, time);
}
protected boolean navigationClick(int status, int time) {
onClick();
return true;
}
protected abstract void onClick();
}
}
答案 4 :(得分:0)
Screen1.java
package com.app;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class Screen1 extends MainScreen {
private VerticalFieldManager mainManager;
private NavigationMenuBar tabBar;
private static Screen1 ref= null;
public static Screen1 getInstance(){
if(ref==null){
ref = new Screen1();
}
return ref;
}
public Screen1() {
tabBar = new NavigationMenuBar() {
public void onNavigationClick(int selection) {
if (NavigationMenuBar.TAB1 == selection) {
} else if (NavigationMenuBar.TAB2 == selection) {
// Push Screen Which is releted to second tab
synchronized (Application.getEventLock()) {
Screen2.getInstance().setSelectedTab(NavigationMenuBar.TAB2);
UiApplication.getUiApplication().pushScreen(
Screen2.getInstance());
}
}
}
};
tabBar.selectTab(NavigationMenuBar.TAB1);
mainManager = new VerticalFieldManager() {
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(maxWidth, maxHeight);
setExtent(getPreferredWidth(), getPreferredHeight());
}
public int getPreferredWidth() {
return Display.getWidth();
}
public int getPreferredHeight() {
return Display.getHeight() - tabBar.getPreferredHeight();
}
};
// setTitle("Screen one");
mainManager.add(new ButtonField("Screen1"));
add(mainManager);
add(tabBar);
}
public void setSelectedTab(int tabNumber) {
tabBar.selectTab(tabNumber);
}
}
SCreen2.java
package com.app;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class Screen2 extends MainScreen {
private VerticalFieldManager mainManager;
private NavigationMenuBar tabBar;
private static Screen2 ref= null;
public static Screen2 getInstance(){
if(ref==null){
ref = new Screen2();
}
return ref;
}
public Screen2() {
tabBar = new NavigationMenuBar() {
public void onNavigationClick(int selection) {
if (NavigationMenuBar.TAB1 == selection) {
// Push Screen Which is releted to first tab
synchronized (Application.getEventLock()) {
Screen1.getInstance().setSelectedTab(NavigationMenuBar.TAB1);
UiApplication.getUiApplication().pushScreen(
Screen1.getInstance());
}
} else if (NavigationMenuBar.TAB2 == selection) {
}
}
};
tabBar.selectTab(NavigationMenuBar.TAB2);
mainManager = new VerticalFieldManager() {
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(maxWidth, maxHeight);
setExtent(getPreferredWidth(), getPreferredHeight());
}
public int getPreferredWidth() {
return Display.getWidth();
}
public int getPreferredHeight() {
return Display.getHeight() - tabBar.getPreferredHeight();
}
};
// setTitle("Screen two");
mainManager.add(new ButtonField("Screen2"));
add(mainManager);
add(tabBar);
}
public void setSelectedTab(int tabNumber) {
tabBar.selectTab(tabNumber);
}
}
请在NAvigationMenuScreen中注释这两行
tab1.setFocus();
tab2.setFocus();