Android 以编程方式按下imageButton不会';我不能立即执行

Android 以编程方式按下imageButton不会';我不能立即执行,android,Android,每当用户选择按钮时,都会调用playTune。问题是行myButton.setPressed(true)仅在播放乐曲后调用。这违背了逻辑,所以我想知道Android是否会将事件排成队列?我希望行myButton.setPressed(true)在代码中显示时调用。i、 在演奏音符之前 private void playTune() { isTunePlaying = true; //Get no of notes user selected String selectedValu

每当用户选择按钮时,都会调用playTune。问题是行
myButton.setPressed(true)仅在播放乐曲后调用。这违背了逻辑,所以我想知道Android是否会将事件排成队列?我希望行
myButton.setPressed(true)在代码中显示时调用。i、 在演奏音符之前

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();     
} 
private void playTune(){
isTunePlaying=true;
//获取用户选择的notes的数目
字符串selectedValue=notecontvalues[notecontidx];
Log.v(Log_标签,“selectedValue:+selectedValue+”notecontidx:+notecontidx);
国际努夫诺特斯酒店;
如果(“全部”。等于(selectedValue)){
noOfNotes=50;
}
否则{
noOfNotes=Integer.parseInt(selectedValue);
}
TuneManager TuneManager=TuneManager.getInstance(this);
Log.v(Log_标签,“tuneNamesIdx:+tuneNamesIdx”);
Tune-Tune=tuneManager.getTune(tuneNamesIdx+1);
Log.v(Log_标记,“tuneTitle:+tune.getTitle()+”tuneNoOfNotes:+tune.getNotes().size());
//初始化预期注释
expectedNotes=新的StringBuffer();
//记笔记并演奏
List notes=tune.getNotes();
//for(注意:tune.getNotes()){

对于(int i=1;i不确定这是否是您正在运行的,但是如果您导致事件驱动的事件(发生在UI线程上)当然,只有在当前事件处理程序返回后,才能在UI线程上处理它。

是的。这有一定的道理。我可能没有给你多少时间来想出解决方案。用户按下“播放调整”按钮。这可以通过调用上面的播放调整方法来处理,该方法在一个注释列表中运行,听起来像eac依次是h。从您的回答中,我假设对setPressed的调用发生在UI线程上,该线程必须等待playTune按钮完成其处理。然而,我将playTune按钮放在其自己的处理程序中,但仍然会遇到问题public void onClick(View v){Log.v(Log_标记),View id为:“+v.getId()”;if(v.getId())==R.id.play_tune_按钮{Handler myHandler=new Handler();myHandler.post(new Runnable(){public void run(){Log.v(Log_标记,“In Handler new Thread”);playTune();});Log.v(Log_标记,“OTHER Thread”);}有什么想法吗?解决了!当监听屏幕事件的onClick按钮时,在非UIThread中执行:否则如果(v.getId()==R.id.play\u tune\u button){//触发一个线程来做一些我们不应该直接在UI线程中做的工作t=new thread(){public void run(){playTune();}};t.start();}但是当突出显示一个便笺时,使用rununuithread(new Runnable(){…等等)获取UIThread的句柄。我要去酒吧喝一杯!