2016-09-27 76 views
0

我正在使用簡單的語音識別。從文本視圖中的語音識別打印文本,第一個除外

我知道它成功地工作。

可以說我講「備註嘿,這是堆棧溢出」

我要排除「備註」,並打印文本的休息一個TextView。

以下是我的工作onActivityResult: -

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可以在這裏很有用,不能夠了解如何與我的代碼中使用它。有人可以提供我的文章或幫助我確定邏輯嗎?

回答

2

你可以通過拆分方法來完成。

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

現在arr[0]將包含"Remark"arr[1]將包含"hey this is stack overflow"