希望这能更好地解释它。 所以我有10个基本相同的东西。 每个类有十个按钮,每个按钮启动一个语音识别意图,以及一个startActivityForResult。
onClickListeners和onAcitivityResults位于GlobalListener类上,该类由10个活动中的每个活动进行扩展。
下面是'GlobalListener.java'的代码
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
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, "Speak now.");
startActivityForResult(a, checka);
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
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, "Speak Now.");
startActivityForResult(b, checkb);
在同一个班级,我有onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
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("apples") || results.contains("tree") || results.contains("house") || results.contains("big house")|| results.contains("big tree")|| results.contains("big apple tree")){
/////Do something
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("oranges") || results.contains("orange tree") || results.contains("orange house")|| results.contains("big orange house")|| results.contains("big orange tree")){ ///
我很难尝试构建全局字符串以匹配此参数。
if(results.contains("apples") || results.contains("tree") || results.contains("house") || results.contains("big house")|| results.contains("small house")|| results.contains("big tree"))
由于这10个活动中的每一个都扩展了相同的GlobalListener,我需要在运行时更改GlobalListener“监听”的内容,具体取决于调用这10个活动中的哪个。所以我创建了一个GlobalVariables
类
public class GlobalVariables {
private static GlobalVariables instance;
static {
instance = new GlobalVariables();
}
private GlobalVariables() {
}
public static GlobalVariables getInstance() {
return GlobalVariables.instance;
}
private String someVariable;
public String getSomeVariable() {
return someVariable;
}
public void setSomeVariable(String someVariable) {
this.someVariable = someVariable;
}
所以在我的主要Activity1上我设置它就像这样..
public class Activity1 extends GlobalListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
GlobalVariables.getInstance().setSomeVariable("apples");
在我拥有OnActivityResult的GlobalListener类中,我从
更改它 if(results.contains("apples") || ////
到
if(results.contains(GlobalVariables.getInstance().getSomeVariable()) || ///
这有效,但这意味着我必须创建;
-a onActivityResult
上每个可接受结果的全局字符串 - 设置我的Main类中的字符串
然后在我的onActivityResult上执行results.contains(lobalVariables.getInstance()。getSomeVariable2())等
我不太关心用这种方式写作的时间,而是学习更高效,更清洁的事情。 我想学习如何使用参数构建全局字符串数组?
感谢