2014-06-15 37 views
0

我正在嘗試製作一個按鈕,當它按下時顯示一條消息,但無法讓它正常工作。如何使一個按鈕在黑莓中做點什麼

這裏是我的按鈕:

HorizontalFieldManager buttonManager = 
    new HorizontalFieldManager(ButtonField.FIELD_HCENTER); 

messageButton = new ButtonField("Press me", ButtonField.CONSUME_CLICK); 
messageButton.setChangeListener(this); 
buttonManager.add(messageButton); 
add(buttonManager); 

這裏是一個打印消息的方法:

public void fieldChanged(Field field, int context) { 
    if (field == messageButton) { 
     showMessage(); 
    } 
} 

private void showMessage() { 
    Dialog.inform("The button was pressed"); 
} 

難道我做錯事的showMessage()方法,或標識在別處錯誤?

+0

我建議把對話框放在if語句之外,以確保調用fieldChanged()方法。確保你只有一個fieldChanged()方法。 –

+0

我測試過這段代碼,它工作正常。你的問題在別處。 –

回答

0

檢查事件日誌(Alt + LGLG),我懷疑你會看到有關在非事件線程上推送模態屏幕的錯誤。如果是這樣的話只是改變showMessage

private void showMessage() 
{ 
    UiApplication.getUiApplication.invokeLater(new Runnable() 
    { 
     public void run() 
     { 
     Dialog.inform("The button was pressed"); 
     } 
    }); 
} 
+0

fieldChanged(..)在Event Thread上運行,所以這不是問題。 –

0

我想問題可能是CONSUME_CLICK要傳遞給構造函數的標誌。如果你想用按鈕做某件事,這當然不是你想要使用的標誌。

除此之外,我建議不要使用FieldChangeListener作爲字段。也許按鈕是好的,但對於其他組件,可能會在佈局過程中調用fieldChanged方法而無需用戶交互。這就是爲什麼你幾乎總是想要過濾那些第二個參數(代碼中的上下文)等於FieldChangeListener.PROGRAMMATIC的調用。

更好的選擇是覆蓋navigationClick,這也適用於觸覺屏幕和常規屏幕。

1
// just paste this class and run 







package mypackage; 

import java.io.IOException; 

import javax.microedition.media.MediaException; 
import javax.microedition.media.Player; 

import net.rim.device.api.ui.Field; 
import net.rim.device.api.ui.FieldChangeListener; 
import net.rim.device.api.ui.UiApplication; 
import net.rim.device.api.ui.component.ButtonField; 
import net.rim.device.api.ui.component.Dialog; 
import net.rim.device.api.ui.container.HorizontalFieldManager; 
import net.rim.device.api.ui.container.MainScreen; 


public final class MyScreen extends MainScreen 
{ 

    public MyScreen() 
    {   
     // Set the displayed title of the screen  


     HorizontalFieldManager horizontalFieldManager= new HorizontalFieldManager(); 
     ButtonField buttonField= new ButtonField("click to show dialog",Field.FIELD_VCENTER); 
     horizontalFieldManager.add(buttonField); 
     buttonField.setChangeListener(buttonchangelisners); 
     add(horizontalFieldManager); 

    } 

    private FieldChangeListener buttonchangelisners= new FieldChangeListener() { 

     public void fieldChanged(Field field, int context) { 
      // TODO Auto-generated method stub 

      showDialog("show your message"); 
     } 
    }; 

    public void showDialog(final String message) { 
     UiApplication.getUiApplication().invokeLater(new Runnable() { 
      public void run() { 
       Dialog.alert(message); 
      } 
     }); 
    } 
} 
0

invokeLater的是仍然有用,因爲fieldChanged是在UI引擎行動鏈中的某個時刻被調用,你最好讓它顯示對話框之前完成這項工作。我懷疑不使用invokeLater是造成這個問題的主要原因。

使用fieldChangeListener時CONSUME_CLICK實際上是必需的,否則將調用上下文菜單。如果您切換到在按鈕字段中使用navigationClick,則可以返回true以實現相同的行爲(儘管我更喜歡使用navigationUnclick)。

+0

我想這是迴應我對另一個答案的評論。我的觀點是,其他答案是錯誤的,所提出的改變實際上並不能解決問題。至於在顯示對話框之前是否應該讓Ui交互完成,這取決於您的情況。 –