以编程方式按下imageButton不会立即执行

时间:2010-11-20 00:21:10

标签: android

只要用户选择按钮,就会调用

playTune。问题是只有在播放曲调后才会调用myButton.setPressed(true);行。这违背了逻辑,所以我想知道Android是否排队队列中的事件?我希望在代码中显示行myButton.setPressed(true);。即在播放音符之前。

private void playTune() {

  isTunePlaying = true;

  //Get no of notes user selected
  String selectedValue = noteCountValues[noteCountIdx];
  Log.v(LOG_TAG, "selectedValue:"+selectedValue+" noteCountIdx:"+noteCountIdx);
  int noOfNotes;
  if ("ALL".equals(selectedValue)){
   noOfNotes = 50;
  }
  else{
   noOfNotes = Integer.parseInt(selectedValue);
  }

  TuneManager tuneManager = TuneManager.getInstance(this);
  Log.v(LOG_TAG, "tuneNamesIdx:"+tuneNamesIdx);   
  Tune tune = tuneManager.getTune(tuneNamesIdx+1);

  Log.v(LOG_TAG, " tuneTitle:"+tune.getTitle()+" tuneNoOfNotes:"+tune.getNotes().size());

  //Initialise expectedNotes
  expectedNotes = new StringBuffer();

  //Get notes and play
  List<Note> notes = tune.getNotes();  
  //for (Note note:tune.getNotes()){
  for (int i=1; i<=notes.size() && i<=noOfNotes; i++){

   Log.v(LOG_TAG, "i:"+i+" notesSize:"+notes.size()+" noOfNotes:"+noOfNotes);   

   //Highlight note
   if (isHighlightNotesOn){    
    final View myButton = (ImageButton) findViewById(R.id.middle_c);    
    myButton.setPressed(true);       
   }    

   Note note = notes.get(i-1);
   int notePos = soundManager.getNotePosition(note);
   Log.v(LOG_TAG, "current note:"+note.getName()+" playing notePos:"+notePos+" setting duration:"+note.getDurationMS());   
   soundManager.playSound(notePos);

   //Add to expectedNotes
   expectedNotes.append(" "+note.getName()+",");   

   //Sleep for the duration
   try{
    Thread.currentThread().sleep(note.getDurationMS());//sleep for 1000 ms
   }
   catch(InterruptedException ie){

   }
  }

  isTunePlaying = false;  

  //Initialise actualNotesPlayed i.e. start from after the tine has finished
  actualNotesPlayed = new StringBuffer();     
} 

1 个答案:

答案 0 :(得分:1)

不确定这是否是您正在遇到的问题,但是如果您从事件驱动的事件(在UI线程上发生)导致事件,则当然无法在您当前的事件处理程序返回之前在UI线程上处理它。 / p>