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

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

4933 1

前面三个章节把程序的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

文章评分1次,平均分5.0

本文原始地址:https://www.tiandiyoyo.com/2009/11/%e9%bb%91%e8%8e%93%e5%bc%80%e5%8f%91%e6%96%b0%e6%89%8b%e5%85%a5%e9%97%a8%e6%95%99%e5%ad%a6%e5%b8%96%ef%bc%8c%e5%a6%82%e4%bd%95%e5%88%b6%e4%bd%9c%e4%b8%80%e4%b8%aa%e8%83%bd%e6%8e%a7%e5%88%b6led-4/
本站所有文章,除了特别注明外,均为本站原创,转载请注明出处来自www.tiandiyoyo.com

您可能还会对以下文章感兴趣:

评论前先开启评论开关:


1 Comment

  1. 郑永 :

    很久没有接触黑莓了。

载入分页评论...