2012-07-25 44 views
0

从这个代码,无法执行另一个按钮的功能,如果当前按钮集中

public class Custom_TopField extends Manager { 
private Bitmap download = Config_GlobalFunction.Bitmap("btn_download.png"); 
private Bitmap downloadactive = Config_GlobalFunction 
     .Bitmap("btn_download_active.png"); 
private Bitmap refresh = Config_GlobalFunction.Bitmap("icon_refresh.png"); 
private Bitmap refreshactive = Config_GlobalFunction 
     .Bitmap("icon_refresh_active.png"); 
private Bitmap back = Config_GlobalFunction.Bitmap("btn_back.png"); 
private Bitmap backctive = Config_GlobalFunction 
     .Bitmap("btn_back_active.png"); 
private Bitmap news = Config_GlobalFunction.Bitmap("icon_news.png"); 
private Bitmap newsactive = Config_GlobalFunction 
     .Bitmap("icon_news_active.png"); 

private Custom_ButtonField downloadbtn, refreshbtn, backbtn, newsbtn; 
private Custom_LabelField title; 
private int left, right, fontsize; 
private Database_Webservice webservice; 

Custom_TopField(final MainScreen mainscreen, final int position, 
     final int catsid, final String header, int left, int right) { 
    super(Manager.USE_ALL_WIDTH | Manager.NO_VERTICAL_SCROLL 
      | Manager.NO_HORIZONTAL_SCROLL); 
    this.left = left; 
    this.right = right; 

    if (Display.getWidth() > 480) 
     fontsize = 43; 
    else if (Display.getWidth() < 481 && Display.getWidth() > 320) 
     fontsize = 33; 
    else 
     fontsize = 23; 

    webservice = new Database_Webservice(); 
    setBackground(Config_GlobalFunction.loadbackground(Display.getWidth() 
      + "_" + "header_bar.png")); 

    if (position != 0) { 
     title = new Custom_LabelField(Config_GlobalFunction.maintitle, 
       DrawStyle.ELLIPSIS | LabelField.USE_ALL_WIDTH 
         | DrawStyle.HCENTER | Field.FOCUSABLE, Color.WHITE) { 
      protected boolean navigationClick(int status, int time) { 
       Main.getUiApplication().pushScreen(
         new Custom_LoadingScreen(1)); 
       Main.getUiApplication().invokeLater(new Runnable() { 
        public void run() { 
         Main.getUiApplication().pushScreen(
           new Main_AllLatestNews(false)); 
        } 
       }, 1 * 1000, false); 
       return true; 
      } 

      protected boolean touchEvent(TouchEvent message) { 
       int eventCode = message.getEvent(); 
       if (eventCode == TouchEvent.UNCLICK) 
        Main.getUiApplication().pushScreen(
          new Menu_PopupMenu(position)); 

       return true; 
      } 
     }; 

    } else { 
     title = new Custom_LabelField(Config_GlobalFunction.maintitle, 
       DrawStyle.ELLIPSIS | LabelField.USE_ALL_WIDTH 
         | DrawStyle.HCENTER, Color.WHITE); 
    } 
    title.setFont(Font.getDefault().derive(Font.BOLD, fontsize)); 
    add(title); 

    if (left == 1) { 
     newsbtn = new Custom_ButtonField(news, newsactive, newsactive) { 
      protected boolean navigationClick(int status, int time) { 
       Main.getUiApplication().pushScreen(
         new Menu_PopupMenu(position)); 
       return true; 
      } 

      protected boolean touchEvent(TouchEvent message) { 
       int eventCode = message.getEvent(); 
       if (eventCode == TouchEvent.UP) 
        Main.getUiApplication().pushScreen(
          new Menu_PopupMenu(position)); 

       return true; 
      } 
     }; 

     add(newsbtn); 
    } else if (left == 2) { 
     backbtn = new Custom_ButtonField(back, backctive, backctive) { 
      protected boolean navigationClick(int status, int time) { 
       Main.getUiApplication().popScreen(mainscreen); 
       return true; 
      } 

      protected boolean touchEvent(TouchEvent message) { 
       int eventCode = message.getEvent(); 
       if (eventCode == TouchEvent.UP) 
        Main.getUiApplication().popScreen(mainscreen); 
       return true; 
      } 
     }; 
     add(backbtn); 
    } 

    if (right == 1) { 
     downloadbtn = new Custom_ButtonField(download, downloadactive, 
       downloadactive) { 
      protected boolean navigationClick(int status, int time) { 
       if (Config_GlobalFunction 
         .Dialog(Config_GlobalFunction.alertdownload)) { 
        if (Config_GlobalFunction.isConnected()) { 
         webservice.UpdateAllCatNews(); 
        } else 
         Config_GlobalFunction.Message(
           Config_GlobalFunction.nowifi, 1); 
       } else 
        Config_GlobalFunction.CloseDialog(); 
       return true; 
      } 

      protected boolean touchEvent(TouchEvent message) { 
       int eventCode = message.getEvent(); 
       if (eventCode == TouchEvent.UP) { 
        if (Config_GlobalFunction 
          .Dialog(Config_GlobalFunction.alertdownload)) { 
         if (Config_GlobalFunction.isConnected()) { 
          webservice.UpdateAllCatNews(); 
         } else 
          Config_GlobalFunction.Message(
            Config_GlobalFunction.nowifi, 1); 
        } else 
         Config_GlobalFunction.CloseDialog(); 
       } 
       return true; 
      } 
     }; 
     add(downloadbtn); 
    } else if (right == 2) { 
     refreshbtn = new Custom_ButtonField(refresh, refreshactive, 
       refreshactive) { 
      protected boolean navigationClick(int status, int time) { 
       if (Config_GlobalFunction.isConnected()) { 
        webservice 
          .refreshCatNewsindex(catsid, position, header); 
       } else 
        Config_GlobalFunction.Message(
          Config_GlobalFunction.nowifi, 1); 
       return true; 
      } 

      protected boolean touchEvent(TouchEvent message) { 
       int eventCode = message.getEvent(); 
       if (eventCode == TouchEvent.UP) { 
        if (Config_GlobalFunction.isConnected()) 
         webservice.refreshCatNewsindex(catsid, position, 
           header); 
        else 
         Config_GlobalFunction.Message(
           Config_GlobalFunction.nowifi, 1); 
       } 
       return true; 
      } 
     }; 
     add(refreshbtn); 
    } 
} 
} 

有一个在左一个按钮newsbtndownloadbtn右侧。当newsbtn是重点,我点击downloadbtn,它不会提示Dialog,但它会pushscreen。对,如果我点击downloadbtn,它会提示Dialog而不是pushscreen

当两个按钮都没有对焦时,我点击按钮,该按钮正在对焦并执行预期。

我试着对焦默认按钮,这是ButtonField,然后我点击另一个字段,它正确执行。

我认为是custom_buttonfield类问题。

这是我的Custom_FieldButton

public class Custom_ButtonField extends ButtonField { 
Bitmap mNormal; 
Bitmap mFocused; 
Bitmap mActive; 

int mWidth; 
int mHeight; 

private int color = -1; 
String text; 

public Custom_ButtonField(Bitmap normal, Bitmap focused, Bitmap active) { 
    super(CONSUME_CLICK | Field.FOCUSABLE | Field.FIELD_HCENTER 
      | Field.FIELD_VCENTER); 
    mNormal = normal; 
    mFocused = focused; 
    mActive = active; 
    mWidth = mNormal.getWidth(); 
    mHeight = mNormal.getHeight(); 
    setMargin(0, 0, 0, 0); 
    setPadding(0, 0, 0, 0); 
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0))); 
    setBorder(VISUAL_STATE_ACTIVE, 
      BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0))); 
} 

public Custom_ButtonField(String text, Bitmap normal, Bitmap focused, 
     Bitmap active, int color) { 
    super(CONSUME_CLICK | Field.FOCUSABLE | Field.FIELD_HCENTER 
      | Field.FIELD_VCENTER); 
    this.color = color; 
    mNormal = normal; 
    mFocused = focused; 
    mActive = active; 
    mWidth = mNormal.getWidth(); 
    mHeight = mNormal.getHeight(); 
    setMargin(0, 0, 0, 0); 
    setPadding(0, 0, 0, 0); 
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0))); 
    setBorder(VISUAL_STATE_ACTIVE, 
      BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0))); 
    this.text = text; 
} 

public Custom_ButtonField(String text, Bitmap normal, Bitmap focused, 
     Bitmap active, int color, long style) { 
    super(style); 
    this.color = color; 
    mNormal = normal; 
    mFocused = focused; 
    mActive = active; 
    mWidth = mNormal.getWidth(); 
    mHeight = mNormal.getHeight(); 
    setMargin(0, 0, 0, 0); 
    setPadding(0, 0, 0, 0); 
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0))); 
    setBorder(VISUAL_STATE_ACTIVE, 
      BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0))); 
    this.text = text; 
} 

public void setText(String text) { 
    this.text = text; 
    invalidate(); 
} 

public String getText() { 
    return text; 
} 

public void setColor(int color) { 
    this.color = color; 
} 

protected void onFocus(int direction) { 
    super.onFocus(direction); 
    color = 0x540604; 
    this.invalidate(); 
} 

protected void onUnfocus() { 
    super.onUnfocus(); 
    color = Color.WHITE; 
    this.invalidate(); 
} 

protected void paint(Graphics graphics) { 
    int fontcontent; 
    if (Display.getWidth() > 480) 
     fontcontent = 28; 
    else if (Display.getWidth() < 481 && Display.getWidth() > 320) 
     fontcontent = 23; 
    else 
     fontcontent = 18; 

    Bitmap bitmap = null; 
    switch (getVisualState()) { 
    case VISUAL_STATE_NORMAL: 
     bitmap = mNormal; 
     break; 
    case VISUAL_STATE_FOCUS: 
     bitmap = mFocused; 
     break; 
    case VISUAL_STATE_ACTIVE: 
     bitmap = mActive; 
     break; 
    default: 
     bitmap = mNormal; 
    } 
    setBackground(BackgroundFactory.createBitmapBackground(bitmap)); 
    graphics.setFont(Font.getDefault().derive(Font.PLAIN, fontcontent)); 
    graphics.setColor(color); 
    graphics.drawText(text, (mNormal.getWidth() - Font.getDefault() 
      .getAdvance(text))/2, ((mNormal.getHeight() - Font 
      .getDefault().getHeight())/2) + 10, DrawStyle.HCENTER 
      | DrawStyle.VCENTER); 
} 

public int getPreferredWidth() { 
    return mWidth; 
} 

public int getPreferredHeight() { 
    return mHeight; 
} 

protected void layout(int width, int height) { 
    setExtent(mWidth, mHeight); 
} 
} 

谁能帮我编辑类,以便该按钮的* 焦点状态不会影响功能? *

回答

0

使用setFieldChangeListener将做所有的行动。

if (position != 0) { 
     title = new Custom_LabelField(Config_GlobalFunction.maintitle, 
       DrawStyle.ELLIPSIS | LabelField.USE_ALL_WIDTH 
         | DrawStyle.HCENTER | Field.FOCUSABLE 
         | ButtonField.CONSUME_CLICK, Color.WHITE) { 
      protected boolean navigationClick(int status, int time) { 
       Main.getUiApplication().pushScreen(
         new Custom_LoadingScreen(1)); 
       Main.getUiApplication().invokeLater(new Runnable() { 
        public void run() { 
         Main.getUiApplication().pushScreen(
           new Main_AllLatestNews()); 
        } 
       }, 1 * 1000, false); 
       return true; 
      } 
     }; 
    } else { 
     title = new Custom_LabelField(Config_GlobalFunction.maintitle, 
       DrawStyle.ELLIPSIS | LabelField.USE_ALL_WIDTH 
         | DrawStyle.HCENTER, Color.WHITE); 
    } 
    title.setFont(Font.getDefault().derive(Font.BOLD, fontsize)); 
    add(title); 

    if (left == 1) { 
     newsbtn = new Custom_ButtonField(news, newsactive, newsactive); 
     newsbtn.setChangeListener(new FieldChangeListener() { 
      public void fieldChanged(Field field, int context) { 
       Main.getUiApplication().pushScreen(
         new Menu_PopupMenu(position)); 
      } 
     }); 
     add(newsbtn); 
    } else if (left == 2) { 
     backbtn = new Custom_ButtonField(back, backctive, backctive); 
     backbtn.setChangeListener(new FieldChangeListener() { 
      public void fieldChanged(Field field, int context) { 
       Main.getUiApplication().popScreen(mainscreen); 
      } 
     }); 
     add(backbtn); 
    } 

    if (right == 1) { 
     downloadbtn = new Custom_ButtonField(download, downloadactive, 
       downloadactive); 
     downloadbtn.setChangeListener(new FieldChangeListener() { 
      public void fieldChanged(Field field, int context) { 
       if (Config_GlobalFunction 
         .Dialog(Config_GlobalFunction.alertdownload)) { 
        if (Config_GlobalFunction.isConnected()) { 
         webservice.UpdateAllCatNews(); 
        } else 
         Config_GlobalFunction.Message(
           Config_GlobalFunction.nowifi, 1); 
       } else 
        Config_GlobalFunction.CloseDialog(); 
      } 
     }); 
     add(downloadbtn); 
    } else if (right == 2) { 
     refreshbtn = new Custom_ButtonField(refresh, refreshactive, 
       refreshactive); 
     refreshbtn.setChangeListener(new FieldChangeListener() { 
      public void fieldChanged(Field field, int context) { 
       if (Config_GlobalFunction.isConnected()) { 
        Config_GlobalFunction.Message(
          Config_GlobalFunction.refreshing, 1); 
        webservice.UpdateMoreCatNews(catsid, position, header); 
       } else 
        Config_GlobalFunction.Message(
          Config_GlobalFunction.nowifi, 1); 
      } 
     }); 
     add(refreshbtn); 
    } 
} 

但是,有一件坏事是当你点击或使用拨轮点击按钮时,它会出现蓝色。

0

我知道你已经发布了一个答案,但我想建议另一种选择。尝试下载BlackBerry Advanced UI Samples。在有很多有用的自定义类Field类,几乎每个应用程序需要做的事情。

More information here

Custom_ButtonField类,你有没有很多在它的代码,所以我可能会建议开始与BitmapButtonField类是在高级UI样品文件夹(/src/com/samples/toolkit/ui/component下)。

如果您需要添加一些颜色绘画代码,看起来您已经对此感到满意。

我认为关键的是,对于大多数Field子类,你可能不应该重写touchEvent()方法。你可以,但它比大多数人想象的更棘手。这对于复杂的领域非常有用,其中触摸的确切坐标很重要,或者您正在处理自定义滑动手势。

如果您完全不实施touchEvent()方法,则可以使用navigationClick()navigationUnClick()中的代码来满足您的需要。

但是,我认为你是在正确的轨道上。一般来说,你将有一个可重用的按钮类(如BitmapButtonField),它不知道点击时该做什么。我通常让一个Manager类注册一个FieldChangeListener做点什么,当按钮被点击。这就是你的高级UI样品中看到:

BitmapButtonField one = new BitmapButtonField(
      Bitmap.getBitmapResource("button_back_normal.png"), 
      Bitmap.getBitmapResource("button_back_focus.png")); 
    add(one); 
    one.setChangeListener(new FieldChangeListener() { 
     public void fieldChanged(Field f, int context) { 
      Dialog.alert("Hello Button!"); 
     } 
    }); 

BitmapButtonField已正确设置处理触摸点击,并跟踪轮点击,也不会弄乱聚焦状态。

尝试从此开始,看看它是否更好。祝你好运!