开发者

Issue with ImageButton.setVisibility()

开发者 https://www.devze.com 2022-12-27 19:44 出处:网络
I\'m having a problem when setting the visibility of two image buttons one on top of the other. The idea is to implement a play/pause control. The problem is that the only part where s开发者_如何学JAV

I'm having a problem when setting the visibility of two image buttons one on top of the other. The idea is to implement a play/pause control. The problem is that the only part where s开发者_如何学JAVAetting the visibility actually works is in the click listeners of the buttons. If I try to change it somewhere else nothing happens. Any idea why is this happening?

playBtn.setOnClickListener(new OnClickListener() {//PLAY BUTTON LISTENER
    public void onClick(View v) {
 playBtn.setVisibility(ImageButton.GONE);
 pauseBtn.setVisibility(ImageButton.VISIBLE);
 mp.start();
    }});

pauseBtn.setOnClickListener(new OnClickListener() {//PAUSE BUTTON LISTENER
 public void onClick(View v) {
  pauseBtn.setVisibility(ImageButton.GONE);
  playBtn.setVisibility(ImageButton.VISIBLE);
  mp.pause();
 }});

final class SeekBarTask extends TimerTask {
  public SeekBarTask(int duration) {

  }
  @Override
  public void run() {
   if(seekBar.getProgress() >= mp.getDuration()) {//IF SONG HAS FINISHED...
    pauseBtn.setVisibility(ImageButton.GONE);//THESE ONES
    playBtn.setVisibility(ImageButton.VISIBLE);//DOESN'T WORK
    mp.stop();
   }
   else {
   seekBar.incrementProgressBy(100);
   }
  }
 }


I would recommend just changing the icon of one ImageButton.


I would think only one of two things could be happening. Either this code never gets hit, or the variables are not referring to the same object instances you're expecting them to. Have you put a breakpoint inside that condition? I would check that a break point even gets hit in there, and then check that the variables are pointing at the correct button instances.

Without seeing the rest of the code I have to ask...why are you checking on a progress bar for a "finished playing" condition versus using the media players on completion callback?

I'm doing something very similar, and I use the MediaPlayer's OnCompletionListener to flip the visibility of my buttons.


I don't remember the details of Android GUI manipulation but could it have to do that you're doing it from another thread and you're not supposed to?


i noticed that setting an ImageButton to View.INVISIBLE is not working when you have set an Animation to it. you have to remove the Animation then make it Invisible. bad pitfall i think...

0

精彩评论

暂无评论...
验证码 换一张
取 消