如何在Blackberry中自定义水平字段管理器和Click事件中设置焦点?

时间:2012-03-31 09:32:55

标签: blackberry blackberry-simulator

  

嗨,我在屏幕上生成自定义布局,我希望在焦点时更改水平字段管理器的颜色或图像,并执行所选水平字段管理器的Click事件。对于   更多参考资料见屏幕截图。

这是我试过的代码..

HorizontalFieldManager hfmrow=new HorizontalFieldManager(USE_ALL_WIDTH|HorizontalFieldManager.FOCUSABLE | Field.FOCUSABLE)
                             {
                                    public void onFocus(int direction) 
                                      {
                                            isFocus  = true;
                                            super.onFocus(direction);
                                            invalidate();

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



                                    }
                                    public void paint(Graphics g)
                                    {   
                                        if(isFocus) 
                                        { 
                                            hfmrow.setBackground(BackgroundFactory.createSolidBackground(Color.RED));
                                            invalidate();                                                                                    
                                            super.paint(g);

                                        } 
                                        else 
                                        { 
                                            hfmrow.setBackground(BackgroundFactory.createSolidBackground(Color.GREEN));                                         
                                            invalidate();                                        
                                            super.paint(g); 
                                        }
                                    }

                                protected boolean navigationClick(int status, int time) 
                                {
                                    Dialog.alert("You are in hfm"+id);
                                    return true;
                                }

                             };                         


                            hfmrow.setMargin(0, 10, 0, 10);
                            hfmrow.add(new NullField(Field.FOCUSABLE));

任何帮助都是值得欣赏的.. Thanx提前

enter image description here

2 个答案:

答案 0 :(得分:2)

在这里你怎么做这个代码用于在你选择hfm时改变颜色

import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.Touchscreen;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.NullField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;

public class sample extends MainScreen implements FieldChangeListener{

HorizontalFieldManager logInDetailManager=null;
int background_color=0;

public sample() {

    logInDetailManager = new HorizontalFieldManager(Manager.USE_ALL_WIDTH|Field.FOCUSABLE){
        protected void sublayout(int maxWidth, int maxHeight) {
                int height=40;
            super.sublayout(Display.getWidth(), height);
            setExtent(Display.getWidth(), height);
        }
        protected void paint(Graphics graphics) {

            graphics.setBackgroundColor(background_color);
            graphics.clear();
            invalidate();
            super.paint(graphics);
        }
        protected void onFocus(int direction) {
            super.onFocus(direction);
            background_color=Color.RED;
            invalidate();
        }
        protected void onUnfocus() {
            invalidate();
            background_color=Color.GREEN;

        }
        protected boolean navigationClick(int status, int time) {

            if(Touchscreen.isSupported()){
                return false;
            }else{
                fieldChangeNotify(1);
                return true;
            }

        }
        protected boolean touchEvent(TouchEvent message)
        {
            if (TouchEvent.CLICK == message.getEvent())
            {
                FieldChangeListener listener = getChangeListener();
                if (null != listener)
                    this.setFocus();
                    listener.fieldChanged(this, 1);
            }
            return super.touchEvent(message);
        }
    };
    logInDetailManager.setChangeListener(this);
    logInDetailManager.add(new LabelField("hello"));
    logInDetailManager.add(new NullField(Field.FOCUSABLE));
    add(logInDetailManager);
    add(new LabelField("good",Field.FOCUSABLE));    
}

public void fieldChanged(Field field, int context) {            
    if(field==logInDetailManager){                  
        UiApplication.getUiApplication().invokeLater(new Runnable() {                          
            public void run() {                                 
                Dialog.inform("Hi how are you?");                          
            }                    
        });          
    }   
}

}

答案 1 :(得分:1)

使用onFocus()onUnfocus将焦点保持在HorizontalFieldManager。并使用navigationClick()点击你的hfm。但请确保您在Field.FOCUSABLE中设置HorizontalFieldManager

选择一个boolean来检查焦点状态并在paint方法中执行代码。检查以下代码..

`

 boolean _isFocus = false;
        HorizontalFieldManager hfm = new HorizontalFieldManager(HorizontalFieldManager.FOCUSABLE | Field.FOCUSABLE)
            {
            public void onFocus(int direction) 
              {
                _isFocus  = true;
                    super.onFocus(direction);
              }
            public void onUnfocus() 
            {

  _isFocus  = false;
            super.onUnfocus();

        }
public void paint(Graphics g)
                {   
                    if(_isFocus)
                    {
                        // replace your bitmap image or bgcolor here
                                             hfm.invalidate();
                        super.paint(g);
                    }
                else
                {
                    // replace your bitmap image or bgcolor here
                                             hfm.invalidate();
                    super.paint(g);
                }
            }

        protected boolean navigationClick(int status, int time) 
        {
            Dialog.alert("You are in hfm");
            return true;
        }
    }`

希望它有效!