アプリ作成中です。画面横向き設定。
画面左側はアニメーションが動きます。
画面右側は操作・問題画面。計6つ(上・下・左・右・スタート・やり直し)のボタンで操作。
<実現したいこと>
「せいかい!」・「ざんねん!」のアラートダイアログの発生を数秒遅らせること
<試したこと>
Handlerを用いて処理を遅延させようとしたが,
new Handler().postDelayed(new Runnable() {
のHandlerという部分に赤文字エラーが発生してしまった。(エラー内容:Cannot resolve symbol 'Handler')
java
1 2public class MainActivity extends AppCompatActivity implements Animator.AnimatorListener { 3 4 final static float TRANSLATE_X = 310.0f; // 1回の操作でX軸方向にいくつ動かすか 5 final static float TRANSLATE_Y = 310.0f; // 1回の操作でY軸方向にいくつ動かすか 6 7 // 盤面上の開始座標を初期値として設定する 8 private int x = 0; 9 private int y = 0; 10 private String text; 11 private ArrayList<Integer> History; // 履歴を格納するリスト 12 private boolean ReplayFlag; // 再生中であることを示すフラグ 13 private int ReplayCount; // 再生経過のカウンター 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 20 Button btn1 = (Button) findViewById(R.id.button1); // うえ 21 Button btn2 = (Button) findViewById(R.id.button2); // した 22 Button btn3 = (Button) findViewById(R.id.button3); // ひだり 23 Button btn4 = (Button) findViewById(R.id.button4); // みぎ 24 Button btn5 = (Button) findViewById(R.id.button5); // やりなおし 25 Button btn6 = (Button) findViewById(R.id.button6); // スタート 26 27 text = ""; 28 final TextView result = (TextView) findViewById(R.id.result); 29 result.setText(""); 30 31 // 初期値をセット 32 History = new ArrayList<>(); 33 ReplayFlag = false; 34 ReplayCount = 0; 35 36 37 btn1.setOnClickListener(new View.OnClickListener() { 38 @Override 39 public void onClick(View v) { 40 41 // 「うえ」を押したときの処理 42 /*if (y > -3) {*/ 43 TextView result = (TextView) findViewById(R.id.result); 44 text += "<font color=#98fb98><b>↑</b></font>"; 45 result.setText(Html.fromHtml(text)); 46 History.add(0); // 0は上を意味する 47 } 48 }); 49 btn2.setOnClickListener(new View.OnClickListener() { 50 @Override 51 public void onClick(View v) { 52 53 // 「した」を押したときの処理 54 /*if (y < 0) {*/ 55 TextView result = (TextView) findViewById(R.id.result); 56 text += "<font color=#ffff99><b>↓</b></font>"; 57 result.setText(Html.fromHtml(text)); 58 History.add(1); // 1は下を意味する 59 } 60 }); 61 btn3.setOnClickListener(new View.OnClickListener() { 62 @Override 63 public void onClick(View v) { 64 65 // 「ひだり」を押したときの処理 66 /*if (x > 0) {*/ 67 TextView result = (TextView) findViewById(R.id.result); 68 text += "<font color=#ffc0cb><b>←</b></font>"; 69 result.setText(Html.fromHtml(text)); 70 History.add(2); // 2は左を意味する 71 } 72 }); 73 btn4.setOnClickListener(new View.OnClickListener() { 74 @Override 75 public void onClick(View v) { 76 77 // 「みぎ」を押したときの処理 78 /*if (x < 2) {*/ 79 TextView result = (TextView) findViewById(R.id.result); 80 text += "<font color=#afeeee><b>→</b></font>"; 81 result.setText(Html.fromHtml(text)); 82 History.add(3); // 3は右を意味する 83 } 84 }); 85 btn5.setOnClickListener(new View.OnClickListener() { 86 @Override 87 public void onClick(View v) { 88 x = 0; 89 y = 0; 90 animationImageReset(); 91 History.clear(); 92 93 text = ""; 94 TextView result = (TextView) findViewById(R.id.result); 95 result.setText(""); 96 } 97 }); 98 btn6.setOnClickListener(new View.OnClickListener() { 99 @Override 100 public void onClick(View v) { 101 // isEmpty()でリストが空っぽではない(1手以上の記録がある)か調べる 102 if (!History.isEmpty()) { 103 // 猫を最初の位置に戻す 104 x = 0; 105 y = 0; 106 animationImageReset(); 107 108 109 ReplayFlag = true; // 再生中にする 110 animationReplay(); // 再生する(ここでは1手目だけ、以後はonAnimationEndに任せる) 111 } 112 } 113 }); 114 } 115 116 private void animationImageView(String property, float start, float end) { 117 ImageView imageView = (ImageView) findViewById(R.id.MyCat); 118 ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, property, start, end); 119 animator.setDuration(1000); 120 animator.addListener(this); 121 animator.start(); 122 } 123 124 private void animationImageReset() { 125 final ImageView MyCat = (ImageView) findViewById(R.id.MyCat); 126 ObjectAnimator.ofFloat(MyCat, "translationX", 0.0f, 0.0f).start(); 127 ObjectAnimator.ofFloat(MyCat, "translationY", 0.0f, 0.0f).start(); 128 } 129 130 private void encountDog() { 131 if (x == 1 && y == -2) { 132 new Handler().postDelayed(new Runnable() { 133 @Override 134 public void run() { 135 AlertDialog.Builder dl = new AlertDialog.Builder(MainActivity.this); 136 dl.setTitle("ざんねん!!"); 137 dl.setMessage("いぬはしょうがいぶつだよ"); 138 dl.setPositiveButton("OK", new DialogInterface.OnClickListener() { 139 @Override 140 public void onClick(DialogInterface dialog, int which) { 141 x = 0; 142 y = 0; 143 animationImageReset(); 144 History.clear(); 145 146 text = ""; 147 TextView result = (TextView) findViewById(R.id.result); 148 result.setText(""); 149 } 150 }); 151 dl.show(); 152 153 } 154 }, 1000); 155 156 } 157 if (x == 2 && y == -3) { 158 new Handler().postDelayed(new Runnable() { 159 @Override 160 public void run() { 161 AlertDialog.Builder dl = new AlertDialog.Builder(MainActivity.this); 162 dl.setTitle("せいかい!!"); 163 dl.setPositiveButton("こたえ", new DialogInterface.OnClickListener() { 164 @Override 165 public void onClick(DialogInterface dialog, int which) { 166 AlertDialog.Builder dl = new AlertDialog.Builder(MainActivity.this); 167 dl.setTitle("こたえ"); 168 dl.setMessage("①→→↑↑↑②↑↑↑→→③→↑→↑↑など"); 169 dl.setPositiveButton("つぎのもんだいへすすむ", null); 170 dl.show(); 171 } 172 }); 173 dl.setNegativeButton("つぎのもんだいへすすむ", null); //ボタン 174 dl.setNeutralButton("プログラムのかいせつ",null); 175 dl.show(); 176 177 } 178 }, 1000); 179 } 180 } 181 182 private void animationReplay() { 183 if (ReplayFlag) { 184 int dir = History.get(ReplayCount); // リストから取り出し 185 186 switch (dir) { 187 case 0: // 0は上を意味する 188 upAnimation(); 189 break; 190 case 1: // 1は下を意味する 191 downAnimation(); 192 break; 193 case 2: // 2は左を意味する 194 leftAnimation(); 195 break; 196 case 3: // 3は右を意味する 197 rightAnimation(); 198 break; 199 } 200 201 ReplayCount++; // カウンターを増やす 202 203 // 再生終了したらフラグをクリアする 204 if (ReplayCount == History.size()) { 205 ReplayFlag = false; 206 ReplayCount = 0; 207 } 208 if (x == 1 && y == -2) { 209 ReplayFlag = false; 210 ReplayCount = 0; 211 } 212 } 213 } 214 215 @Override 216 public void onAnimationStart(Animator animation) { 217 } 218 219 @Override 220 public void onAnimationEnd(Animator animation) { 221 // アニメーションが終了するとここが実行される 222 if (ReplayFlag) { 223 animationReplay(); // 再生する 224 } 225 } 226 227 @Override 228 public void onAnimationCancel(Animator animation) { 229 } 230 231 @Override 232 public void onAnimationRepeat(Animator animation) { 233 } 234 235 private void upAnimation() { 236 // 「うえ」を押したときの処理 237 if (y > -3) { 238 float start = TRANSLATE_Y * y; 239 float end = start - TRANSLATE_Y; 240 animationImageView("translationY", start, end); 241 y--; 242 encountDog(); 243 } 244 } 245 246 private void downAnimation() { 247 // 「した」を押したときの処理 248 if (y < 0) { 249 float start = TRANSLATE_Y * y; 250 float end = start + TRANSLATE_Y; 251 animationImageView("translationY", start, end); 252 y++; 253 encountDog(); 254 } 255 } 256 257 private void leftAnimation() { 258 // 「ひだり」を押したときの処理 259 if (x > 0) { 260 float start = TRANSLATE_X * x; 261 float end = start - TRANSLATE_X; 262 animationImageView("translationX", start, end); 263 x--; 264 encountDog(); 265 } 266 } 267 268 private void rightAnimation() { 269 // 「みぎ」を押したときの処理 270 if (x < 2) { 271 float start = TRANSLATE_X * x; 272 float end = start + TRANSLATE_X; 273 animationImageView("translationX", start, end); 274 x++; 275 encountDog(); 276 } 277 } 278}
回答1件
あなたの回答
tips
プレビュー