Android Global Class, extend Application, Helper Class
关于使用全局变量类和扩展应用程序的另一个问题。希望它变化足够大,不被认为是复制品。我读过这些伟大的问题和答案。
异步编程最佳实践
如何在Android中使用全局类变量
虽然我知道这些与我目前的问题有什么关系,但由于我是一个新手,我有两个主要问题。
1-我不太确定如何应用这些解决方案,但我会自己解决。
第二个也是最大的问题是,我应该尝试将代码的哪一部分迁移到全局类或助手类。
所以我有3个类,分别是section1.java、section2.java、section3.java,它们以相同的方式,使用相同的方法,基本上都使用相同的XML布局(每个XML更改中只有XML的名称和drawable的with)。唯一的区别是他们接受的结果是正确的。
关于我的第二个关注点,我的问题在粘贴的代码下面。
这是来自1.java部分的代码(粗略的,looong spagethi代码)
1.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | public class Section1 extends Activity implements OnClickListener, OnInitListener{ private TextToSpeech myTTS; ListView lv; private ProgressDialog dialog; static final int check = 111; static final int checka = 222; static final int checkb = 333; TextView speak; private int MY_DATA_CHECK_CODE = 0; int counter_score; RatingBar rb_cooking; MediaPlayer ourSong; ImageView display,image1,image2,image3; int gotLetterA, gotLetterB,gotLetterC int counter_score_a; int counter_score_b; int counter_score_c; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.section1); Intent checkTTSIntent = new Intent(); checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE); TextView speak = (TextView) findViewById(R.id.text_to_speech); //////////////////////////////////// counter_score=0; counter_score_this=0; counter_score_b=0; counter_score_c=0; image1 = (ImageView) findViewById(R.id.IVimage_1); image2 = (ImageView) findViewById(R.id.IVimage_2); image3 = (ImageView) findViewById(R.id.IVimage_3); initialize(); private void initialize() { // TODO Auto-generated method stub lv = (ListView)findViewById(R.id.lvVoiceReturn); speak = (TextView) findViewById(R.id.text_to_speech); Button play1 = (Button) findViewById(R.id.play_01); Button play2 = (Button) findViewById(R.id.play_2); Button play3 = (Button) findViewById(R.id.play_3); image1.setOnClickListener(this); image2.setOnClickListener(this); image3.setOnClickListener(this); play1.setOnClickListener(this); play2.setOnClickListener(this); play3.setOnClickListener(this); public void speakClicked(View v) { speak.setText("testing text"); speak(false); } public void speakJbClicked(View v) { speak(true); } private void speak(boolean b) { // get the text entered TextView speak = (TextView) findViewById(R.id.texto_correcto); speak.setText("test, spoken text"); String words = speak.getText().toString(); speakWords(words, b); } private void speakWords(String speech, boolean b) { HashMap<String, String> hashMap = new HashMap<String, String>(); hashMap.put(TextToSpeech.Engine.KEY_FEATURE_NETWORK_SYNTHESIS,"true"); myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, b ? hashMap : null); } @Override public void onClick(View v) { if (v.getId() == R.id.IVimage_1){ Intent a = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); a.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); a.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); a.putExtra(RecognizerIntent.EXTRA_PROMPT,"Speech rec prompt."); startActivityForResult(a, checka); }else if(v.getId() == R.id.IVimage_2){ Intent b = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); b.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); b.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); b.putExtra(RecognizerIntent.EXTRA_PROMPT,"Speech rec prompt."); startActivityForResult(b, checkb); }else if(v.getId() == R.id.IVimage_3){ Intent c = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); c.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); c.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); c.putExtra(RecognizerIntent.EXTRA_PROMPT,"Speech rec prompt."); startActivityForResult(c, checkc); }else if(v.getId() == R.id.play_01){ myTTS.setLanguage(Locale.US); myTTS.setPitch((float) .9); myTTS.setSpeechRate((float) 0.75); myTTS.speak("a",TextToSpeech.QUEUE_FLUSH, null); }else if(v.getId() == R.id.play_2){ myTTS.setLanguage(Locale.US); myTTS.setPitch((float) .9); myTTS.setSpeechRate((float) 0.75); myTTS.speak("b",TextToSpeech.QUEUE_FLUSH, null); }else if(v.getId() == R.id.play_3){ myTTS.setLanguage(Locale.US); myTTS.setPitch((float) .9);/ myTTS.setSpeechRate((float) 0.75); myTTS.speak("c",TextToSpeech.QUEUE_FLUSH, null); @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub if (requestCode == MY_DATA_CHECK_CODE) { // if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) { // the user has the necessary data - create the TTS myTTS = new TextToSpeech(this, this); myTTS.setPitch((float) .9);/////delete myTTS.setSpeechRate((float) 0.75);///delete maybe // } // else { // //no data - install it now // Intent installTTSIntent = new Intent(); // installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); // startActivity(installTTSIntent); // } } if (requestCode == checka && resultCode == RESULT_OK){ ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results)); if(results.contains("hey") || results.contains("a") || results.contains("ay") || results.contains("add")|| results.contains("day")||results.contains("A")){ counter_score ++; image1.setVisibility(View.INVISIBLE); ourSong = MediaPlayer.create(Section1.this, R.raw.rightsound2); ourSong.start(); AlertDialog dialogBuilder = new AlertDialog.Builder(this).create(); dialogBuilder.setTitle("Great"); dialogBuilder.setMessage("some msg"); dialogBuilder.setIcon(R.drawable.ic_mark); dialogBuilder.setButton("Continue", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialogBuilder.show(); Thread timer = new Thread(){ public void run(){ try{ sleep(2500); }catch (InterruptedException e){ e.printStackTrace(); } finally { if(counter_score == 3){ Bundle basket = new Bundle(); basket.putInt("key", counter_score); Intent nextAct = new Intent(Section1.this, Result_Section1.class); nextAct.putExtras(basket); startActivity(nextAct); finish(); } } } }; timer.start(); }else{ lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results)); counter_score +=0; ourSong = MediaPlayer.create(Section1.this, R.raw.wrongsound); ourSong.start(); AlertDialog dialogBuilder = new AlertDialog.Builder(this).create(); dialogBuilder.setTitle("Oops"); dialogBuilder.setMessage("Wrong msg"); dialogBuilder.setIcon(R.drawable.ic_wrong); dialogBuilder.setButton("Try again", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialogBuilder.show(); Thread timer = new Thread(){ public void run(){ try{ sleep(4000); }catch (InterruptedException e){ e.printStackTrace(); } finally { } } }; timer.start(); } } if (requestCode == checkb && resultCode == RESULT_OK){ ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results)); if(results.contains("b") || results.contains("be") || results.contains("boob")|| results.contains("dude")|| results.contains("B")){ counter_score ++; image2.setVisibility(View.INVISIBLE); ourSong = MediaPlayer.create(Section1.this, R.raw.rightsound2); ourSong.start(); AlertDialog dialogBuilder = new AlertDialog.Builder(this).create(); dialogBuilder.setTitle("Great"); dialogBuilder.setMessage("some msg"); dialogBuilder.setIcon(R.drawable.ic_mark); dialogBuilder.setButton("Continue", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialogBuilder.show(); Thread timer = new Thread(){ public void run(){ try{ sleep(2500); }catch (InterruptedException e){ e.printStackTrace(); } finally { if(counter_score == 3){ Bundle basket = new Bundle(); basket.putInt("key", counter_score); Intent nextAct = new Intent(Section1.this, Result_Section1.class); nextAct.putExtras(basket); startActivity(nextAct); finish(); } } } }; timer.start(); }else{ lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results)); counter_score +=0; ourSong = MediaPlayer.create(Section1.this, R.raw.wrongsound); ourSong.start(); AlertDialog dialogBuilder = new AlertDialog.Builder(this).create(); dialogBuilder.setTitle("Oops"); dialogBuilder.setMessage("Wrong msg"); dialogBuilder.setIcon(R.drawable.ic_wrong); dialogBuilder.setButton("Try again", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialogBuilder.show(); Thread timer = new Thread(){ public void run(){ try{ sleep(4000); }catch (InterruptedException e){ e.printStackTrace(); } finally { } } }; timer.start(); } } if (requestCode == checkc && resultCode == RESULT_OK){ ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results)); if(results.contains("c") || results.contains("see") || results.contains("sea")|| results.contains("C")){ counter_score ++; image3.setVisibility(View.INVISIBLE); ourSong = MediaPlayer.create(Section1.this, R.raw.rightsound2); ourSong.start(); AlertDialog dialogBuilder = new AlertDialog.Builder(this).create(); dialogBuilder.setTitle("Great"); dialogBuilder.setMessage("some msg"); dialogBuilder.setIcon(R.drawable.ic_mark); dialogBuilder.setButton("Continue", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialogBuilder.show(); Thread timer = new Thread(){ public void run(){ try{ sleep(2500); }catch (InterruptedException e){ e.printStackTrace(); } finally { if(counter_score == 3){ Bundle basket = new Bundle(); basket.putInt("key", counter_score); Intent nextAct = new Intent(Section1.this, Result_Section1.class); nextAct.putExtras(basket); startActivity(nextAct); finish(); } } } }; timer.start(); }else{ lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results)); counter_score +=0; ourSong = MediaPlayer.create(Section1.this, R.raw.wrongsound); ourSong.start(); AlertDialog dialogBuilder = new AlertDialog.Builder(this).create(); dialogBuilder.setTitle("Oops"); dialogBuilder.setMessage("Wrong msg"); dialogBuilder.setIcon(R.drawable.ic_wrong); dialogBuilder.setButton("Try again", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialogBuilder.show(); Thread timer = new Thread(){ public void run(){ try{ sleep(4000); }catch (InterruptedException e){ e.printStackTrace(); } finally { } } }; timer.start(); } } super.onActivityResult(requestCode, resultCode, data); } @Override public void onInit(int status) { // check for successful instantiation // if (initStatus == TextToSpeech.SUCCESS) { // if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE) ///myTTS.setLanguage(Locale.US); // } // else if (initStatus == TextToSpeech.ERROR) { // Toast.makeText(this,"Sorry! Text To Speech failed...", // Toast.LENGTH_LONG).show(); // } } @Override protected void onDestroy() { // TODO Auto-generated method stub if (myTTS !=null){ myTTS.stop(); myTTS.shutdown(); } super.onDestroy(); } public void onBackPressed() { finish(); } } |
我将发布第2.java节和第3.java节,但代码中只有4个内容发生了变化。
1-XML布局,它本身就是一个精确的副本。
setContentView(R.layout.section1);//changes to setContentView(R.layout.section2),setContentView(R.layout.section2) accordingly
2-被接受的结果是对所说内容的正确回答。
if(results.contains("hey") || results.contains("a") || results.contains("ay") || results.contains("add")|| results.contains("day")||results.contains("A")){//to whatever the correct answer is for that class
3-移动到下一个活动所需的数量问题
if(counter_score == 3){
4-发送我的包的活动
Intent nextAct = new Intent(Section1.this, Result_Section1.class);
我知道我可以为变量构建一个全局类。
所以最后我的问题。
正如我在这里读到的,我不能为alertdialog构建一个全局类,因为如果我正确理解了对话框,就需要一个上下文,这是view类的一部分。
我可以建立一个
正如我在这里读到的,我知道我可以构建一个全局类来处理onclickListener。
问题2.我可以在那个全球级别上使用
回答Q1。创建视图类的对象时,将视图类的上下文传递给对话框类。像这样使用构造器
1 2 3 4 5 6 7 | public DialogClass { final Context mContext; DialogClass(Context context) { mContext = context; } } |
并在从视图类传递上下文时创建它的对象。