黑莓开发新手入门教学帖,如何制作一个能控制LED颜色的程序(四)

来源:互联网 发布:淘宝艾滋病试纸可靠吗 编辑:程序博客网 时间:2024/05/01 13:22

前面三个章节把程序的UI差不多都设计完毕了,只缺一个按钮来让程序根据第三课中选择的颜色来进行LED的操作。于是我们先添加一个按钮

ButtonField mySubmitButton = new ButtonField("Submit",ButtonField.CONSUME_CLICK );
add(mySubmitButton);

然后通过一个listener来监听这个按钮的事件。当用户点击这个按钮submit的时候,我们先去判断之前的颜色单选项哪一项被选中,然后分别给出相应的颜色。a1,a2的值是上文提到的LED灯亮和灯灭的时间长度,由用户输入。在给颜色之前,我们用if(LED.isPolychromatic())来判断该设备是否支持LED灯,如果支持,才给颜色,让LED变化色彩。

mySubmitButton.setChangeListener(new FieldChangeListener() {

    public void fieldChanged(Field field, int context) {
        int a1 = Integer.parseInt(editfield2.getText());
        int a2 = Integer.parseInt(editfield3.getText());

        if (rbField1.isSelected()){
            Dialog.inform("Change Led to RED");
            if(LED.isPolychromatic())
                LED.setColorConfiguration(a1, a2, 0x00FF0000);
        }

        else if (rbField2.isSelected()){
            Dialog.inform("Change Led to ORANGE");
            if(LED.isPolychromatic())
                 LED.setColorConfiguration(a1, a2, 0x00FF6100);
                }

         ....

         ....

}

image

LED的document,让我们看下官方API说明就会一目了然了。

setColorConfiguration
public static void setColorConfiguration(int onTime,                                         int offTime,                                         int color)
Configures the status LED.

If the LED is not capable of displaying multiple colors, the color parameter will be ignored, and the LED will display at BRIGHTNESS_25.

Parameters:
onTime - Time in milliseconds the LED should turn on for if blinking.
offTime - Time in milliseconds the LED should turn off for if blinking.
color - Color to use, of the form 0x00RRGGBB.
Throws:
IllegalArgumentException - if onTime is negative
IllegalArgumentException - if offTime is negative
IllegalArgumentException - if color is invalid
Since:
JDE 4.0.0
原创粉丝点击