带有ObjectListField的复选框不具有可焦点性和可检查性? [英] Checkbox with a ObjectListField not focusable and checkable?

查看:117
本文介绍了带有ObjectListField的复选框不具有可焦点性和可检查性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个复选框,并且在此对象列表下方.我正在尝试着眼于复选框,但它既不可聚焦也不可检查.焦点位于下面列表的第一个元素上.

I am trying to create a checkbox and below this objectlist is there. I am trying to focus on checkbox but it is niether focusable nor checkable .The focus stands on the first element of the list below.

如何集中显示复选框并使之可检查?在这里,复选框位于我要添加列表的VerticalFieldManager中.

How I can focus the checkbox as well as making it checkable? Here, checkbox is in a VerticalFieldManager in which I am adding the list.

在这里我要添加两件事:一个是Horizo​​ntal Field Manager(onlineCheckBoxHfm),另一个是objectListField(buddyList).

Here I am adding two things:one is Horizontal Field Manager(onlineCheckBoxHfm) and other is objectListField(buddyList).

现在的问题是,默认焦点位于列表的第一个元素上,而我无法将焦点放在放置CheckBox的Horizo​​ntalFieldManager中.由于焦点在Horizo​​ntalFieldManager上不可用,因此我无法检查CheckBox.

Now problem is, default focus is on first element of the list and I am unable to get focus on HorizontalFieldManager in which I have put the CheckBox. Since focus is not available on HorizontalFieldManager so I am unable to check the 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类如下:

BuddyListField class is as follows:

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;
        }
    }
}

推荐答案

我无法运行您的代码,因为其中没有太多代码(其他类,PNG等),但是我会敢打赌您的问题在这里:

I can't run your code, because there's too much in there that I don't have (other classes, PNGs, etc.), but I would be willing to bet that your problem is here:

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

   // other stuff here

      listField.setFocus();

调用setFocus()会将焦点设置在列表字段(BuddyListField)上.因此,您的CheckBoxField 实际上是可以聚焦的.问题是您添加了复选框字段,然后添加了列表字段.使用drawListRow()绘制列表字段时,您总是将焦点从复选框移开,然后将其放回列表字段.

Calling setFocus() will set the focus on the list field (the BuddyListField). So, your CheckBoxField is actually focusable. The problem is that you add the checkbox field, and then add the listfield. When the listfield is drawn, with drawListRow(), you are always taking focus away from the checkbox and putting it back on the listfield.

删除对listField.setFocus()的调用...其中有两个.

Remove that call to listField.setFocus() ... there are two of them.

drawListRow()用于绘制.我不确定我能想到为什么该方法中的任何内容实际上应更改焦点(setFocus()所做的事情)的原因.

drawListRow() is meant for drawing. I'm not sure I can think of a reason why anything in that method should actually change the focus, which is what setFocus() does.

这篇关于带有ObjectListField的复选框不具有可焦点性和可检查性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆