2

我知道有很多关于语音识别的话题和问题,我的问题也可能是一个愚蠢的问题。但请大家多多包涵。

我需要将语音识别的结果放入(可编辑文本框)而不是(数组列表)中,该可编辑文本框允许用户编辑结果,就像备忘录一样。

我发现了一些类似我的问题,但我无法理解,与你们相比,我仍然是初学者。

这是代码:

public class AVRScreen extends Activity {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;

private ListView mlvTextMatches;
private Button mbtSpeak;
private Button reButton;
private EditText result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vr_screen);
Toast.makeText(this, "Press Speak! to Start Speeking",
         Toast.LENGTH_LONG).show();

result = (EditText) findViewById(R.id.out_text); 
mlvTextMatches = (ListView) findViewById(R.id.lvTextMatches);
mbtSpeak = (Button) findViewById(R.id.btSpeak);
reButton = (Button)findViewById(R.id.Replay1);
reButton.setOnClickListener(new OnClickListener(){


    public void onClick(View v) {

        startActivity(new Intent(v.getContext(),KeyBoard.class));
    } 
 });


checkVoiceRecognition();
}

public void checkVoiceRecognition() {
// Check if voice recognition is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0) {
mbtSpeak.setEnabled(false);
mbtSpeak.setText("Voice recognizer not present");
Toast.makeText(this, "Voice recognizer not present",
 Toast.LENGTH_SHORT).show();

 }
 }

public void speak(View view) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);


intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass()
.getPackage().getName());

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

//Start the Voice recognizer activity for the result.
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}          


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)

//If Voice recognition is successful then it returns RESULT_OK
if(resultCode == RESULT_OK) {

ArrayList<String> textMatchList = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

 if (textMatchList.get(0).contains("search")) {

 } else {
     // populate the Matches
     mlvTextMatches .setAdapter(new ArrayAdapter<String>      
(this,android.R.layout.simple_list_item_1,textMatchList));
 }

 }


 //Result code for various error.
{
 } if(resultCode == RecognizerIntent.RESULT_AUDIO_ERROR){
 showToastMessage("Audio Error");
}else if(resultCode == RecognizerIntent.RESULT_CLIENT_ERROR){
 showToastMessage("Client Error");
}else if(resultCode == RecognizerIntent.RESULT_NETWORK_ERROR){
showToastMessage("Network Error");
}else if(resultCode == RecognizerIntent.RESULT_NO_MATCH){
showToastMessage("No Match");
}else if(resultCode == RecognizerIntent.RESULT_SERVER_ERROR){
showToastMessage("Server Error");
}
 super.onActivityResult(requestCode, resultCode, data);
 }
/**
  * Helper method to show the toast message
  **/
  void showToastMessage(String message){
  Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
   }
 }

这是编辑后的代码:

public class AVRScreen extends Activity {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;

private Button mbtSpeak;
private Button reButton;
private EditText myEditText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vr_screen);
Toast.makeText(this, "Press Speak! to Start Speeking",
         Toast.LENGTH_LONG).show();

myEditText = (EditText) findViewById(R.id.out_text); 
mbtSpeak = (Button) findViewById(R.id.btSpeak);
reButton = (Button)findViewById(R.id.Replay1);
reButton.setOnClickListener(new OnClickListener(){


    public void onClick(View v) {

        startActivity(new Intent(v.getContext(),KeyBoard.class));
    } 
 });


checkVoiceRecognition();
}

public void checkVoiceRecognition() {
// Check if voice recognition is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0) {
mbtSpeak.setEnabled(false);
mbtSpeak.setText("Voice recognizer not present");
Toast.makeText(this, "Voice recognizer not present",
 Toast.LENGTH_SHORT).show();

 }
 }

public void speak(View view) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);


intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass()
.getPackage().getName());

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

//Start the Voice recognizer activity for the result.
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}          


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)

//If Voice recognition is successful then it returns RESULT_OK
if(resultCode == RESULT_OK) {

ArrayList<String> textMatchList = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

 if (textMatchList.get(0).contains("search")) {

 } else {
    // populate the Matches
    myEditText.setText(textMatchList.toString());
    // if the above does not look good
    // for (String match : textMatchList) {
    //        myEditText.append(match + "\n"); // or whatever separator you want
        //   }
     }

}

第二次尝试是:

 } else {
    // populate the Matches
    //myEditText.setText(textMatchList.toString());
    // if the above does not look good
    for (String match : textMatchList) {
           myEditText.append(match + "\n"); // or whatever separator you want
           }
     }

}
4

1 回答 1

2
    if(resultCode == RESULT_OK) {

ArrayList<String> textMatchList = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

 if (textMatchList.get(0).contains("search")) {

 } else {
     // populate the Matches
    result.setText(textMatchList.toString());
    // if the above does not look good
    // for (String match : textMatchList) {
    //        result.append(match + "\n"); // or whatever separator you want
    //   }
 }
于 2013-05-11T19:41:26.210 回答