0

次のコードは、一部のビューの背景色の明るさを128から255に、またはその逆にするために書いたコードです。残念ながら、それを待たせるハンドラは正しく機能していません。親切にもこのコードを教えてください。ハンドラーがAndroidの関数で正しく機能していない

9個のビューを持つ3x3マトリックスがあります。私はランダムに任意の1つのセルの不透明度を変更しています。

LEVEL:1つずつ変更したいセルの数。ここで、LEVEL:3

color [9]:3x3の9つのビューを含むマトリックス。 AndroidのモニターLogcat

11-06 04:21:27.267 30640-30640/com.example.aman D/ i :: 0 
11-06 04:21:27.267 30640-30640/com.example.aman D/ random :: 1 
11-06 04:21:27.267 30640-30640/com.example.aman D/Pattern incoming: 128 
11-06 04:21:27.267 30640-30640/com.example.aman D/Outside handler: 128 
11-06 04:21:27.267 30640-30640/com.example.aman D/ i :: 1 
11-06 04:21:27.267 30640-30640/com.example.aman D/ random :: 3 
11-06 04:21:27.267 30640-30640/com.example.aman D/Pattern incoming: 128 
11-06 04:21:27.267 30640-30640/com.example.aman D/Outside handler: 128 
11-06 04:21:27.267 30640-30640/com.example.aman D/ i :: 2 
11-06 04:21:27.267 30640-30640/com.example.aman D/ random :: 7 
11-06 04:21:27.267 30640-30640/com.example.aman D/Pattern incoming: 128 
11-06 04:21:27.267 30640-30640/com.example.aman D/Outside handler: 128 
11-06 04:21:27.267 30640-30640/com.example.aman D/ random :: 7 
11-06 04:21:27.267 30640-30640/com.example.aman D/Pattern incoming: 128 
11-06 04:21:27.267 30640-30640/com.example.aman D/Outside handler: 128 
$$ - 11-06 04:21:27.267 30640-30640/com.example.aman D/Inside handler: 128 
$$ - 11-06 04:21:27.267 30640-30640/com.example.aman D/Inside handler: 128 
$$ - 11-06 04:21:27.267 30640-30640/com.example.aman D/Inside handler: 128 

あなたは「インサイドハンドラは」3回走るループの最後に印刷されていることを見ることができるように

public void pattern() { 

    for(int i=0;i<LEVEL;i++) { 
     int rand= 0 + (int)(Math.random() * 8); 
     computer+=rand; 
     Log.d(" i :" , ""+i); 
     Log.d(" random :" , ""+rand); 
     Log.d("Pattern incoming " , ""+color[rand].getBackground().getAlpha()); 
     color[rand].getBackground().setAlpha(128); 

     final int random=rand; 
     handler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       color[random].getBackground().setAlpha(128); 
       Log.d("Inside handler " , ""+color[random].getBackground().getAlpha()); 
       color[random].getBackground().setAlpha(255); 
      } 
     },2000); 

     color[rand].getBackground().setAlpha(128); 

     Log.d("Outside handler " , ""+color[rand].getBackground().getAlpha()); 

    } 
} 

。私は、次のように単に「パターンの着信」の後と「外ハンドラ」の前に実行する「インサイドハンドラ」を期待していた:

11-06 04:21:27.267 30640-30640/com.example.aman D/ i :: 0 
11-06 04:21:27.267 30640-30640/com.example.aman D/ random :: 1 
11-06 04:21:27.267 30640-30640/com.example.aman D/Pattern incoming: 128 
$$ - 11-06 04:21:27.267 30640-30640/com.example.aman D/Inside handler: 128  
11-06 04:21:27.267 30640-30640/com.example.aman D/Outside handler: 128 
11-06 04:21:27.267 30640-30640/com.example.aman D/ i :: 1 
11-06 04:21:27.267 30640-30640/com.example.aman D/ random :: 3 
11-06 04:21:27.267 30640-30640/com.example.aman D/Pattern incoming: 128 
$$ - 11-06 04:21:27.267 30640-30640/com.example.aman D/Inside handler: 128 
11-06 04:21:27.267 30640-30640/com.example.aman D/Outside handler: 128 
11-06 04:21:27.267 30640-30640/com.example.aman D/ i :: 2 
11-06 04:21:27.267 30640-30640/com.example.aman D/ random :: 7 
11-06 04:21:27.267 30640-30640/com.example.aman D/Pattern incoming: 128 
$$ - 11-06 04:21:27.267 30640-30640/com.example.aman D/Inside handler: 128 
11-06 04:21:27.267 30640-30640/com.example.aman D/Outside handler: 128 
+0

正確にlogcatで見たいと思われるものは何ですか?ハンドラが正しく機能していないということをあなたはどういう意味ですか?また、新しいウィンドウで開く必要がないように、質問に貼り付けてください。 –

+0

@DavidRawson。私はここにlogcatステートメントを追加しました。その質問が今はっきりしていることを願っています。あなたの助けは高く評価されます。 –

+0

ありがとう!それは簡単に答えます –

答えて

0

期待どおりlogcatになっている結果です。あなたが書いたものを見ていきましょう:

(各ステートメントが1ミリ秒を要すると想定)

1. first iteration of loop at time 1 ms 
2. print "i :: 0" 
3. print "random :: 1" 
4. print "Pattern incoming :: 128" 
5. post a task to the `Handler` to set the color 2 seconds from now (time 2005 ms) 
6. print "Outside handler :: 128" 
7. go on to next iteration of loop without waiting for the previous task to complete 

8. second iteration of loop at time 8 ms: 
9. print print "i :: 1" 
10. print "random :: 3" 
11. print "Pattern incoming :: 128" 
12. post a task to the `Handler` to set the color 2 seconds from now (time 2012ms) 
.... 
**loop terminates** 
.... 
(finally a long time after the loop has completed the first scheduled task will be triggered) 
.... 
2005. print "Inside handler: 128" 

ループが次の反復に進む前にタスクを完了するためにHandlerを待ちません。

あなたの仕事は、あなたが望む効果を得ることができるようにあなたのコードをリファクタリングすることです。多くのタスクを同期してダンプするのではなく、Handlerのコードを使用して、ある種の繰り返しを実行する必要があります。

+0

ありがとうございます。私は間違いを知った。アプリケーションは現在正しく動作しています。私は再帰を使ってそれをやった。ハンドラ自体の中で関数を呼び出すことは完全に機能しました。 –

+0

よろしくお願いします!あなたは答えを受け入れることができますか? –

+0

はい。絶対に。 –

関連する問題