如何在Blackberry中开发自定义列表字段

时间:2011-08-29 07:52:23

标签: blackberry

在我的应用程序中,我必须在列表字段中显示项目列表,当我单击列表字段中的特定项目时,特定行的背景颜色将更改为灰色。如何在Blackberrry中开发此类型的自定义列表字段。任何人请提出想法。

谢谢

2 个答案:

答案 0 :(得分:1)

你应该在选中的listfield行中绘制矩形..这样的事情。在这里,我已经完成了焦点..

public void drawListRow(ListField list, Graphics g, int index, int y,int w) {

        if (g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS))
        {
            g.setBackgroundColor(0x00572000);
            //g.setBackgroundColor();
             g.clear();
             //g.setColor(Color.BLACK);
            // g.fillRect(0,list.getWidth(),list.getWidth(),80);
             g.setColor(Color.ORANGE);
             g.fillRect(94,y+0,400,30);
             //g.setColor(0x000000);
             g.setColor(Color.WHITE);
             g.drawText(text, 95, y+10, (DrawStyle.LEFT  ), w );
        }
        else
        {
            g.setColor(0x00906966);
             g.fillRect(94,y+0,400,30);
             g.setColor(Color.ORANGE);
             g.drawText(text, 95, y+10, (DrawStyle.LEFT  ), w );    

        }}

答案 1 :(得分:0)

试试这个......

  private class MyListField extends ListField{  


//0,ListField.MULTI_SELECT  
  private boolean hasFocus = false;  

  public  void onFocus(int direction){  
    hasFocus = true;    
  }  

   public void onUnfocus()   
      {  
             hasFocus = false;  
             super.onUnfocus();  
             invalidate();  
      }  

  public void paint(Graphics graphics)   
        {   int width = Display.getWidth();  
            //Get the current clipping region   
            XYRect redrawRect = graphics.getClippingRect();  
            if(redrawRect.y < 0)  
            {  
                throw new IllegalStateException("Error with clipping rect.");  
            }  

            //Determine the start location of the clipping region and end.  
            int rowHeight = getRowHeight();  

            int curSelected;  

            //If the ListeField has focus determine the selected row.  
            if (hasFocus)   
            {  
                 curSelected = getSelectedIndex();  

            }   
            else   
            {  
                curSelected = -1;  
            }  

            int startLine = redrawRect.y / rowHeight;  
            int endLine = (redrawRect.y + redrawRect.height - 1) / rowHeight;  
            endLine = Math.min(endLine, getSize() - 1);  
            int y = startLine * rowHeight;  

            //Setup the data used for drawing.  
            int[] yInds = new int[]{y, y, y + rowHeight, y + rowHeight};  
            int[] xInds = new int[]{0, width, width, 0};  

            //Set the callback - assuming String values.  
            ListFieldCallback callBack = this.getCallback();  

            //Draw each row  
            for(; startLine <= endLine; ++startLine)   
            {                 
           //If the line we're drawing is the currentlySelected line then draw the fill path in LIGHTYELLOW and the   
             //font text in Black.    
           if(startLine == curSelected){  

                   graphics.setColor(Color.LIGHTYELLOW);  
                   graphics.drawFilledPath(xInds, yInds, null, null);  
                   graphics.setColor(Color.BLACK);  
                   graphics.drawText((String)callBack.get(this, startLine), 0, yInds[0]);  

           }  
           else{  
                   //Draw the odd or selected rows.  
                  graphics.setColor(Color.LIGHTGREY);  
                  graphics.drawText((String)callBack.get(this, startLine), 0, yInds[0]);  
           }  

            //Assign new values to the y axis moving one row down.  
              y += rowHeight;  
              yInds[0] = y;  
              yInds[1] = yInds[0];  
              yInds[2] = y + rowHeight;  
              yInds[3] = yInds[2];  
            }  

            //super.paint(graphics);  
        }  
}  

。 。 。 请参阅[LINK]:http://berrytutorials.blogspot.com/2009/11/create-custom-listfield-change.html