3

我正在嘗試創建一個複選框,並且在此對象列表下方存在。我正在努力把重點放在複選框上,但它不是可重點的,也不是可檢查的。重點在於下面列表的第一個元素。複選框ObjectListField不可聚焦和可檢查?

我該如何關注複選框以及使其可檢查?在這裏,複選框位於我添加列表的VerticalFieldManager中。

enter image description here

在這裏,我將兩件事情:一個是水平視場經理(onlineCheckBoxHfm)等是objectListField(好友列表)。

現在的問題是,默認焦點是在列表的第一個元素,我無法得到我已經把CheckBox的Horizo​​ntalFieldManager的焦點。由於FocusFocusManager上的焦點不可用,所以我無法檢查CheckBox。

public class BuddyMenuItem extends VerticalFieldManager implements FieldChangeListener{ 

public BuddyMenuItem(BuddyListField buddyList) { 

     _bitmap = EncodedImage. 
     getEncodedImageResource("buddies_header.png"); 
     mainMenuBmp = new BitmapField(Constant.sizePic(_bitmap, _bitmap.getHeight(), Display.getWidth())); 

     HorizontalFieldManager onlineCheckBoxHfm=new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH); 
     _onlineCheckBox=new CheckboxField("Online",false,CheckboxField.FIELD_LEFT|CheckboxField.FOCUSABLE); 
     _onlineCheckBox.setChangeListener(this); 

     onlineCheckBoxHfm.add(_onlineCheckBox); 
     onlineCheckBoxHfm.add(new NullField(NullField.FOCUSABLE)); 
//  add(mainMenuBmp); 
     add(onlineCheckBoxHfm); 
     add(buddyList); 
    } 

BuddyListField類如下:

import java.util.Vector; 

import net.rim.device.api.system.Bitmap; 
import net.rim.device.api.system.Display; 
import net.rim.device.api.ui.Color; 
import net.rim.device.api.ui.DrawStyle; 
import net.rim.device.api.ui.Graphics; 
import net.rim.device.api.ui.Keypad; 
import net.rim.device.api.ui.TouchEvent; 
import net.rim.device.api.ui.Touchscreen; 
import net.rim.device.api.ui.component.ListField; 
import net.rim.device.api.ui.component.ObjectListField; 

import com.np.naijapings.Buddy; 
import com.np.naijapings.NaijapingsMain; 

public class BuddyListField extends ObjectListField { 
    public static final Bitmap offlineIcon = Bitmap.getBitmapResource("offline.png"); 
    public static final Bitmap awayIcon = Bitmap.getBitmapResource("away.png"); 
    public static final Bitmap busyIcon = Bitmap.getBitmapResource("busy.png"); 
    public static final Bitmap onlineIcon = Bitmap.getBitmapResource("online.png"); 
    public static final Bitmap unreadIcon = Bitmap.getBitmapResource("unread.png"); 
    public static final Bitmap maleIcon=Bitmap.getBitmapResource("male.png"); 

    public static final Bitmap[] statusIcon = new Bitmap[]{offlineIcon, awayIcon, busyIcon, onlineIcon}; 

    public static Vector buddyVector; 
    public static int onlineBuddyCount=0; 
    private final NPingsMain nPings; 
    public static boolean showOnlineBuddy=false; 

    private Bitmap _chatBmp=Bitmap.getBitmapResource("chat.png"); 

    public BuddyListField(NPingsMain n,boolean showOnlineBuddy) { 
     nPings = n; 
     buddyVector = new Vector(); 
     this.showOnlineBuddy=showOnlineBuddy; 
    } 

    public Buddy getBuddyAt(int index) { 
     return (Buddy)buddyVector.elementAt(index);  
    } 

    protected boolean keyChar(char key, int status, int time) { 
     int idx; 
     switch(key) { 
     case Keypad.KEY_ENTER: 
      idx = this.getSelectedIndex(); 
      if (idx >= 0) { 
       NPingsMain.currentBuddy = (Buddy)buddyVector.elementAt(idx); 
       NPingsMain.openBuddy(NPingsMain.currentBuddy); 
      } 
      return true; 

     case 'e': 
      idx = this.getSelectedIndex()-1; 
      if (idx >= 0) { 
       this.setSelectedIndex(idx); 
      } 
      return true; 

     case 's': 
      return true; 

     case 'f': 
      return true; 

     case 'x': 
      idx = this.getSelectedIndex()+1; 
      if (idx > 0 && idx < this.getSize()) { 
       this.setSelectedIndex(idx); 
      } 
      return true; 

     case 't': 
      if (this.getSize() > 0) { 
       this.setSelectedIndex(0); 
      } 
      return true; 
     } 

     return false; 
    } 
    protected boolean navigationClick(int status, int time) { 
     int idx = this.getSelectedIndex(); 
     if(Touchscreen.isSupported()){ 
      if (idx >= 0) { 
       nPings.currentBuddy = (Buddy)buddyVector.elementAt(idx); 
       if((nPings.currentBuddy.status==0x00000003)||(nPings.currentBuddy.status==0x00000002)||(nPings.currentBuddy.status==0x00000001)){ 
        NPingsMain.openBuddy(nPings.currentBuddy); 
       } 
      } 
      return false; 
     }else{ 

      if (idx >= 0) { 
       nPings.currentBuddy = (Buddy)buddyVector.elementAt(idx); 
       if((nPings.currentBuddy.status==0x00000003)||(nPings.currentBuddy.status==0x00000002)||(nPings.currentBuddy.status==0x00000001)){ 
        NPingsMain.openBuddy(nPings.currentBuddy); 
       } 
      } 
      return false; 
     } 
    } 
    protected boolean touchEvent(TouchEvent message) { 
     //if(message.getEvent()==TouchEvent.CLICK){ 
      int idx; 
      switch(TouchEvent.CLICK) { 
      case TouchEvent.CLICK: 
       idx = this.getSelectedIndex(); 
       if (idx >= 0) { 
        nPings.currentBuddy = (Buddy)buddyVector.elementAt(idx); 
        if((nPings.currentBuddy.status==0x00000003)||(nPings.currentBuddy.status==0x00000002)||(nPings.currentBuddy.status==0x00000001)){ 
         NPingsMain.openBuddy(nPings.currentBuddy); 
        } 
       } 
       return true; 

      case 'e': 
       idx = this.getSelectedIndex()-1; 
       if (idx >= 0) { 
        this.setSelectedIndex(idx); 
       } 
       return true; 

      case 's': 
       return true; 

      case 'f': 
       return true; 

      case 'x': 
       idx = this.getSelectedIndex()+1; 
       if (idx > 0 && idx < this.getSize()) { 
        this.setSelectedIndex(idx); 
       } 
       return true; 

      case 't': 
       if (this.getSize() > 0) { 
        this.setSelectedIndex(0); 
       } 
       return true; 
      } 

     // return false; 

    // } 
     return super.touchEvent(message); 
    } 

    public void buddyReposition(Buddy b) { 
     int index = buddyVector.indexOf(b); 
     buddyReposition(index); 
    } 

    public void buddyReposition(int oldIndex) { 
     Buddy b = (Buddy)buddyVector.elementAt(oldIndex); 
     int newIndex = 0; 

     if (b.unread) { 
      newIndex = 0; 
     } else { 
      while (newIndex < buddyVector.size() && 
        ((b == (Buddy)buddyVector.elementAt(newIndex)) || 
          ((Buddy)buddyVector.elementAt(newIndex)).unread || 
          (b.status < ((Buddy)buddyVector.elementAt(newIndex)).status))) 
       ++newIndex; 
     } 

     newIndex = (oldIndex < newIndex) ? (newIndex-1) : newIndex; 

     if (oldIndex != newIndex) { 
      buddyVector.removeElementAt(oldIndex); 
      buddyVector.insertElementAt(b, newIndex); 
     } 

     this.invalidate(); 
    } 

    public int findBuddyIndex(String jid) { 
     for (int i = buddyVector.size()-1; i >= 0; i--) { 
      if (((Buddy)buddyVector.elementAt(i)).jid.equals(jid)) 
       return i; 
     } 

     return -1; 
    } 

    public Buddy findBuddy(String jid) { 
     for (int i = buddyVector.size()-1; i >= 0; i--) { 
      if (((Buddy)buddyVector.elementAt(i)).jid.equals(jid)) 
       return (Buddy)buddyVector.elementAt(i); 
     } 
     return null; 
    } 

    public void addBuddy(Buddy b) { 
     //CHECK FOR ONLINE BUDDIES 
     buddyVector.addElement(b); 
     this.insert(buddyVector.indexOf(b)); 

    } 

    public void deleteBuddy(int index){ 
     buddyVector.removeElementAt(index); 
     this.delete(index); 

    } 

    public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width) { 
     // NOTICE 14 would be consist the icon size 

     onlineBuddyCount=0; 
     String bName=""; 
     Buddy b = (Buddy)buddyVector.elementAt(index); 
     if(showOnlineBuddy==true){ 
      if(b.status==0x00000003||b.status==0x00000002||b.status==0x00000001){ 

       //DRAW ROW BACKGROUND START 
       if (listField.getSelectedIndex() != index) { 
        graphics.setBackgroundColor(index % 2 == 0 ? Color.WHITE 
          : Color.LIGHTGRAY); 
        graphics.clear(); 
        listField.setFocus(); 
       } 
       //DRAW ROW BACKGROUND END 

       onlineBuddyCount++; 
       if(b.name.endsWith("net")){ 
        int i=b.name.indexOf("@"); 
        bName=b.name.substring(0, i); 
       }else{ 
        bName=b.name; 
       } 
       if (b.unread) { 
        graphics.drawBitmap(0, y+3, unreadIcon.getWidth(), unreadIcon.getHeight(),unreadIcon , 0, 0); 
       } else { 
        graphics.drawBitmap(0, y+3, unreadIcon.getWidth(), unreadIcon.getHeight(),statusIcon[b.status] , 0, 0); 
       } 

       graphics.drawText(bName, 25, y, DrawStyle.HDEFAULT, width-12); 
       graphics.drawBitmap(Display.getWidth()-_chatBmp.getWidth(), y-6, _chatBmp.getWidth(), _chatBmp.getHeight(), _chatBmp, 0, 0); 
      } 
     }else{ 
      //DRAW ROW BACKGROUND START 
      if (listField.getSelectedIndex() != index) { 
       graphics.setBackgroundColor(index % 2 == 0 ? Color.WHITE 
         : Color.LIGHTGRAY); 
       graphics.clear(); 
       listField.setFocus(); 
      } 
      //DRAW ROW BACKGROUND END 

      onlineBuddyCount++; 
      if(b.name.endsWith("net")){ 
       int i=b.name.indexOf("@"); 
       bName=b.name.substring(0, i); 
      }else{ 
       bName=b.name; 
      } 
      if (b.unread) { 
       graphics.drawBitmap(0, y+3, unreadIcon.getWidth(), unreadIcon.getHeight(),unreadIcon , 0, 0); 
      } else { 
       graphics.drawBitmap(0, y+3, unreadIcon.getWidth(), unreadIcon.getHeight(),statusIcon[b.status] , 0, 0); 
      } 

      graphics.drawText(bName, 25, y, DrawStyle.HDEFAULT, width-12); 
      graphics.drawBitmap(Display.getWidth()-_chatBmp.getWidth(), y-6, _chatBmp.getWidth(), _chatBmp.getHeight(), _chatBmp, 0, 0); 
     } 
    } 



    public void clearBuddies() { 
     if (nPings.buddyList != null) { 
      int i = buddyVector.size(); 
      while (i-- > 0) 
       this.delete(0); 
      nPings.buddyscreen.delete(nPings.buddyList); 
      nPings.buddyList = null; 
     } 
    } 
} 
+2

請顯示您用來創建CheckBoxField的代碼,並將其添加到管理器(使用ObjectListField)和屏幕。謝謝。 – Nate 2013-03-16 09:22:40

+0

我不認爲它是你展示的代碼中的任何東西。如果'BuddyListField'只是一個'ObjectListField',那麼我可以關注並檢查複選框。也許你可以發佈'BuddyListField'類,並告訴我們BuddyMenuItem *擴展的類是什麼? – Nate 2013-03-19 08:25:50

+0

@Nate:我用BuddyListField類更新了代碼。 – 2013-03-19 09:15:43

回答

1

我不能運行你的代碼,因爲有太多沒有辦法,我沒有(其他類,PNG格式等。 ),但我願意打賭,你的問題就在這裏:

public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width) { 

    // other stuff here 

     listField.setFocus(); 

調用setFocus()本身會將焦點放在列表字段(BuddyListField)上。所以,你的CheckBoxField實際上可以重點。問題是您添加複選框字段,然後添加列表字段。當列表字段被繪製時,用drawListRow(),您總是將焦點從複選框中移開並放回到列表字段中。

刪除那個電話給listField.setFocus() ......有兩個。

drawListRow()是用於繪圖。我不確定我能想出一個理由,爲什麼在該方法中的任何內容實際上應該是更改的重點,這就是setFocus()所做的。

+0

謝謝Nate.It只是因爲** setFocus()**方法,複選框沒有得到focus.I刪除焦點從那裏,現在它的工作。 – 2013-03-21 08:39:00