比较来自语音到文本的短语和来自IF语句的短语



我试图比较TTS和IF语句中的短语。如果短语从TTS等于短语从If语句它必须出现在TextView。如果没有,就不会出现。我试着这样做,但我有一个问题与If语句。即使短语相等或不相等也不会出现。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button recognizeButton = (Button)findViewById(R.id.button1);
    recognizeButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "You may speak!");
    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH);
    startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == RESULT_OK) {
        ArrayList<String> results;
        results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            TextView speechText = (TextView) findViewById(R.id.textView1);
        //if (results.equals("car")) {
            speechText.setText(results.toString());
     //   }
    }
}

}

您正在比较字符串数组与字符串,尝试如下:results.get(0).equals("car"),确保results不是空的。

类似:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if (requestCode == 1 && resultCode == RESULT_OK) {
        ArrayList<String> results;
        results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            TextView speechText = (TextView) findViewById(R.id.textView1);
        if (results != null && results.size() > 0 && results.get(0).equals("car")) {
            speechText.setText(results.toString());
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新