在文本视图中从语音识别中打印文本,第一个除外

时间:2016-09-27 07:40:46

标签: android voice-recognition

我正在使用简单的语音识别。

我成功地工作了。

让我说我说“请注意,这是堆栈溢出”

我想排除“备注”并在TextView中打印其余文本。

以下是我对theActivityResult的工作: -

  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.isEmpty()) {
                if(textMatchList.get(0).contains("Field"))
                {
                    Intent non_field = new Intent(DashboardActivity.this,NonFieldActivity.class);
                    startActivity(non_field);
                }
                else if(textMatchList.get(0).contains("Tour"))
                {
                    Intent non_field = new Intent(DashboardActivity.this,TourPlanActivity.class);
                    startActivity(non_field);
                }
                else {
                    final Dialog list_dialog;
                    ListView voice_match_list;
                    list_dialog = new Dialog(this);
                    list_dialog.setTitle("Matches");
                    list_dialog.setCancelable(true);
                    list_dialog.setContentView(R.layout.voice_list_dialog);
                    voice_match_list = (ListView) list_dialog.findViewById(R.id.voice_list);
                    voice_match_list.setAdapter(new ArrayAdapter<String>(this,
                            android.R.layout.simple_list_item_1,
                            textMatchList));
                    Button cancel_button = (Button) list_dialog.findViewById(R.id.cancel_button_voice);
                    assert cancel_button != null;
                    cancel_button.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {

                            list_dialog.dismiss();
                        }
                    });
                    list_dialog.show();
                }

            }
            //Result code for various error.
        }else 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();
  }

我的谷歌研究: -

.replace在这里很有用,无法理解如何在我的代码中使用它。有人可以向我推荐一些文章或者帮助我解决某些逻辑吗?

1 个答案:

答案 0 :(得分:2)

你可以通过拆分方法来做到这一点。

String message = "Remark hey this is stack overflow";
String [] arr = message.split(" ", 2);

现在arr[0]将包含"Remark"arr[1]将包含"hey this is stack overflow"