首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > BlackBerry >

BlackBerry触摸屏设备编程(二)

2012-12-14 
【分享】BlackBerry触摸屏设备编程(二)三 一个触摸屏幕应用界面的例子 本章以一个定制的黑莓UI展示程序为例,

【分享】BlackBerry触摸屏设备编程(二)
三 一个触摸屏幕应用界面的例子

 

本章以一个定制的黑莓UI展示程序为例,说明黑莓编程中需要注意的各个方面,通过一个可以定制的个性化的toolbar的实现以及黑莓应用的背景的切换,来说明触摸屏幕编程和普通全键盘手机编制程序的区别。

 

黑莓标准的UI组件里面是没有toolbar的,这里要设计一个toolbar,可以考虑标准UI组件的扩展,这里我们让toolbar继承自HorizontalFieldManager.

基本的toolbar的特性包括排列的方向和toolbar的高宽等等以及组件的排列性质。这些都是可以配置的,如果要做到一个比较灵活的设计,这里我们写死。

 

//public class ToolBarField extends HorizontalFieldManager

public class ToolBarField extends HorizontalFieldManager

{

    //private static final int DefaultButtonHeight = 55;

    //private static final int DefaultButtonWidth = 55;

    private static final int DefaultButtonHeight = 129;

    private static final int DefaultButtonWidth = 129;

  

    private Vector leftJustifiedButtons = new Vector();

    private Vector rightJustifiedButtons = new Vector();

    private int preferredHeight = DefaultButtonHeight;

    private int sideMargin = 3;

    private int buttonSpacing = 2;  

    private int preferredWidth = Display.getWidth();

    private Bitmap bg = null;

 

    ……

}

 

在toolbarfield类的设计中,核心的部分在于

    private Vector leftJustifiedButtons = new Vector();

    private Vector rightJustifiedButtons = new Vector();

这两个是为了放置用户加入扩种的具体field组件,这里使用的是标准的vector元素,可以添加也可以删除组件,这里我们简单起见,只实现添加的接口,如下面的addbutton方法所示:

 

    public void addButton(ToolBarButtonField button, boolean leftJustified)

    {

        super.add(button);

       

        if (button.getPreferredHeight() > preferredHeight)

            preferredHeight = button.getPreferredHeight();

      

        if (leftJustified)

        {

            leftJustifiedButtons.addElement(button);

        }

        else

        {

            rightJustifiedButtons.addElement(button);

        }

}

 

为了实现更加可供定制化的效果,可以扩充Horizontalmanager的subpaint方法,添加部分背景处理的能力:

 

    protected void subpaint(Graphics graphics)

    {      

        if (bg != null)

        {

            for (int x = 0; x < Display.getWidth();)



            {

                graphics.drawBitmap(x, 0, getPreferredWidth(), bg.getHeight(), bg, 0, 0);

                x += bg.getWidth();

            }      

        }

        else

        {

            graphics.setColor(Color.BLACK);

            graphics.drawRect(0, 0, getPreferredWidth(), getPreferredHeight());

        }

      

        super.subpaint(graphics);

}

 

如果所有的功能扩充到这里,我们所设计的toolbar也只是一个可供全键盘手机使用的版本,这里我们加入一个新的touchevent方法,用来加入对触摸屏幕的响应有了这个方法,这个就是一个专门针对touchscreen的控件版本版本:

 

    public boolean touchEvent(TouchEvent event)

    {

        int eventID = event.getEvent();

        if (eventID == TouchEvent.DOWN || eventID == TouchEvent.UP)

        {

            boolean hit = false;

            int x = event.getX(1);

            int y = event.getY(1);

          

            for (int f = 0; f < getFieldCount(); f++)

            {

                ToolBarButtonField field = (ToolBarButtonField)getField(f);

                XYRect ext = field.getExtent();

                if (ext.contains(x, y))

                {              

                    hit = true;

 

                   

                    if (eventID == TouchEvent.UP)

                    {

                        field.setActive(false);

                        this.setFocus();



                    }

                    else

                    {

                        field.setFocus();

                        field.setActive(true);                      

                    }

                   

                    invalidate();

                    break;

                }

            }  

          

            if (!hit && eventID == TouchEvent.UP)

            {

                this.setFocus();

                invalidate();

            }

        }

      

        return true;

    }

 

 

基本上UI的展现,如果把如上的部分做完了,有了基本的UI的事件的处理之后也就可以了,系统会自动对UI的布局做一些调整,但是作为一个标准的应用组件,需要考虑的是如何让UI的展现在任何情况下都能达到比较理想的效果,那么就需要对触屏手机的屏幕视图方位切换事件进行相应的处理和考虑了。

 

protected void sublayout(int maxWidth, int maxHeight)

    {  

        this.setExtent(maxWidth, getPreferredHeight());

      

        Enumeration iter = leftJustifiedButtons.elements();      

      

        if (maxHeight > Display.getHeight())

            maxHeight = Display.getHeight();

      

        int y = 0;

        int curX = sideMargin;

      

        //

        // Layout the left justified buttons

        //

        while (iter.hasMoreElements())



        {          

            ToolBarButtonField button = (ToolBarButtonField)iter.nextElement();          

            this.layoutChild(button, button.getPreferredWidth(), button.getPreferredWidth());

                      

            this.setPositionChild(button, curX, y);

            curX = curX + button.getWidth();

        }

      

        int minX = curX + buttonSpacing;

        int totalButtonsWidth = 0;

        //

        // Layout the right justified buttons

        //

        iter = rightJustifiedButtons.elements();

      

        while (iter.hasMoreElements())

        {          

            ToolBarButtonField button = (ToolBarButtonField)iter.nextElement();          

            this.layoutChild(button, button.getPreferredWidth(), button.getPreferredWidth());

            totalButtonsWidth += button.getWidth() + buttonSpacing;          

        }

        totalButtonsWidth -= buttonSpacing;

      

        if ((totalButtonsWidth+minX) > maxWidth)

        {

            preferredWidth = totalButtonsWidth+minX;

        }

        curX = maxWidth;

        iter = rightJustifiedButtons.elements();

        while (iter.hasMoreElements())

        {          

            ToolBarButtonField button = (ToolBarButtonField)iter.nextElement();          

            this.layoutChild(button, button.getPreferredWidth(), button.getPreferredWidth());

          

            curX = curX - button.getWidth();



            this.setPositionChild(button, curX, y);

            curX -= buttonSpacing;          

        }      



 
[解决办法]
收藏,学习学习
[解决办法]
up........
[解决办法]
学习。。
[解决办法]
该回复于2010-09-30 09:46:44被版主删除
[解决办法]
该回复于2010-09-30 10:01:10被版主删除
[解决办法]
完全看不懂哎
[解决办法]
学习下...

[解决办法]
该回复于2010-09-30 09:48:12被版主删除
[解决办法]
学习……
[解决办法]
该回复于2010-10-09 17:31:47被版主删除
[解决办法]
嗬嗬嗬嗬。。。。。。。。。。。。。。。。。太好了谢谢
[解决办法]
该回复于2010-09-30 14:32:08被版主删除
[解决办法]
学习 
学习
[解决办法]
顶一下
[解决办法]
学习了 。。。。
[解决办法]
好好好好
[解决办法]
好好好好
[解决办法]
好好好好
[解决办法]
Thank you
[解决办法]
学习,支持!
[解决办法]
赚点积分用用
[解决办法]
呵呵,学习一下
[解决办法]
感谢中。。。。
[解决办法]
学习下
[解决办法]
不错  努力啊
[解决办法]
该回复于2010-12-03 11:01:36被版主删除
[解决办法]
该回复于2010-12-03 10:31:06被版主删除
[解决办法]
谢谢楼主分享,下来研究一下
[解决办法]
呵呵,学习一下
[解决办法]
学学习习
[解决办法]
该回复于2010-10-08 14:01:08被版主删除
------解决方案--------------------


谢谢LZ,学习了~~
[解决办法]
C++?正在学习中,收藏下。有时间去研究看看
[解决办法]
该回复于2010-11-17 15:46:47被版主删除
[解决办法]
有多学了一点
[解决办法]
不知所言!

热点排行