ブラックベリーJavaでYouTubeのような再生/一時停止ボタンを作成したいのですが、1つのボタンで2つのアクションを処理するにはどうすればよいですか?つまり、ボタンを初めてクリックしたときに音声が再生され、同じボタンをもう一度クリックした場合はオーディオを停止する必要があります。1つのボタンで2つのアクションを処理するにはどうすればよいですか、再生/一時停止などの画像を変更するにはどうすればよいですか?
質問する
292 次
1 に答える
2
import java.util.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.Bitmap;
public class CustomImageButtonApp extends UiApplication
{
static Bitmap image;
public static void main(String[] args)
{
CustomImageButtonApp app = new CustomImageButtonApp ();
app.enterEventDispatcher();
}
public CustomImageButtonApp ()
{
final imagebutton img_btn = new imagebutton("",Field.FOCUSABLE, "play.png",
"pause.png", 0x9cbe95);
MainScreen screen=new MainScreen();
pushScreen(screen);
screen.setTitle("title");
screen.add(img_btn);
}
public class CustomImageButton extends Field {
private String _label;
private int _labelHeight;
private int _labelWidth;
private Font _font;
private Bitmap _currentPicture;
private Bitmap _playPicture;
private Bitmap _pausePicture;
int color;
public CustomImageButton (String text, long style ,String playimg, String pauseimg, int color){
super(style);
_playPicture= Bitmap.getBitmapResource( playimg );
_pausePicture=Bitmap.getBitmapResource( pauseimg );
_font = getFont();
_label = text;
_labelHeight = _playPicture.getHeight();
_labelWidth = _pausePicture.getWidth();
this.color = color;
_currentPicture = _playPicture;
}
/**
* @return The text on the button
*/
String getText(){
return _label;
}
/**
* Field implementation.
* @see net.rim.device.api.ui.Field#getPreferredHeight()
*/
public int getPreferredHeight(){
return _labelHeight;
}
/**
* Field implementation.
* @see net.rim.device.api.ui.Field#getPreferredWidth()
*/
public int getPreferredWidth(){
return _labelWidth;
}
/**
* Field implementation.
* @see net.rim.device.api.ui.Field#drawFocus(Graphics, boolean)
*/
protected void drawFocus(Graphics graphics, boolean on) {
// Do nothing
}
/**
* Field implementation.
* @see net.rim.device.api.ui.Field#layout(int, int)
*/
protected void layout(int width, int height) {
setExtent(Math.min( width, getPreferredWidth()),
Math.min( height, getPreferredHeight()));
}
/**
* Field implementation.
* @see net.rim.device.api.ui.Field#paint(Graphics)
*/
protected void paint(Graphics graphics){
// First draw the background colour and picture
graphics.setColor(this.color);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);
// Then draw the text
graphics.setColor(Color.BLACK);
graphics.setFont(_font);
graphics.drawText(_label, 4, 2,
(int)( getStyle() & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK ),
getWidth() - 6 );
}
/**
* Overridden so that the Event Dispatch thread can catch this event
* instead of having it be caught here..
* @see net.rim.device.api.ui.Field#navigationClick(int, int)
*/
protected boolean navigationClick(int status, int time){
if (_currentPicture == _playPicture) {
_currentPicture = _pausePicture;
// invalidate();
} else {
_currentPicture = _playPicture;
//invalidate();
}
//fieldChangeNotify(1);
return true;
}
}
}
于 2013-02-27T07:52:30.930 に答える