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

时间:2015-08-27 15:13:19

标签: android speech-recognition speech-to-text

我正在尝试比较来自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());
     //   }
    }
}

}

1 个答案:

答案 0 :(得分:1)

您正在将字符串数组与字符串进行比较,尝试类似: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());
        }
    }
}