有关使用全局变量类和扩展应用程序的另一个问题。希望它会有所不同,不要被视为重复。我读过这些很棒的问题和答案。
异步编程最佳实践
如何在Android中使用全局类变量
尽管我了解这些与我当前的问题有何关系,因为我是一个新手,但我有2个主要问题。
1-我不太确定如何应用这些解决方案,但我会尝试自己弄清楚。
2-第二个也是最大的问题是,我的代码的哪一部分什至应该尝试迁移到全球类或助手类。
所以我有3个类,第1章,java,e节。Java,e节。Java,本质上以相同的方式执行相同的操作,使用相同的方法,他们甚至使用几乎精确的XML布局(只有XML的名称(只有XML的名称)每个XML中的可绘制可更改)。唯一的区别是他们接受的结果。
我关于第二点关注点的问题是在粘贴的代码下。
这是来自第1.java的代码(粗糙的,宽松的Spagetthi代码)
节1.java
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();
}
}
我将发布e节。
1- XML布局,本身就是一个精确的副本。
setContentView(r.layout.section1);//更改setContentView(r.layout.section2),setContentView(r.layout.section2)相应地
2-可接受的结果作为所说的正确答案。
if(results.contains(" hey")|| results.contains(" a")|| results.contains(" ay")|| results.contains(" add")|| results.contains("day')|| results.contains(" a")){//对于该类别的正确答案
3 - 搬到下一个活动需要正确的问题
if(counter_score == 3){
4-发送我的捆绑包的活动
intent nextAct = new Intent(第1节,结果_section1.class);
我知道我可以为变量构建全局类。
所以最后我的问题。
正如我在这里阅读的那样,我无法构建一个全局课程,以说明Alertdialog的词,因为如果我理解正确地对话需要上下文,这就是视图类的一部分。
Q1。我应该构建一个public class MyAlertDialogs extends View {
以保持对话框?
正如我在这里阅读的那样,我知道我可以构建一个全局类来处理onclicklistener。
q2。我可以在该全局类上有SpeechRecognizer Intent
或TextToSpeech Intent
吗?
Q1的答案。创建其对象时,将视图类的上下文传递到对话框中。使用
的构造函数public DialogClass {
final Context mContext;
DialogClass(Context context) {
mContext = context;
}
}
并在从视图类传递上下文时创建其对象。