2016-02-08 45 views
0

我有我有TextToSpeech功能來讀很多單詞在編輯文本的應用程序,這是我的代碼:刪除一個字符串的一部分的Android

package com.example.texttoSPCH; 
import android.app.Activity; 
import android.os.Bundle; 
import android.speech.tts.TextToSpeech; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import java.util.Locale; 

public class MyActivity extends Activity implements TextToSpeech.OnInitListener { 
    TextToSpeech tts; 
    EditText edt; 
    Button btn; 
    @Override 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
     init(); 
    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 

     public void onClick(View v) { 

      SpeakOut(); 
     } 

    }); 
} 

private void init() { 
    edt= (EditText) findViewById(R.id.edt); 
    btn= (Button) findViewById(R.id.btn); 
    tts=new TextToSpeech(this,this); 
} 

@Override 
public void onInit(int status) { 
    if (status==TextToSpeech.SUCCESS) { 
     tts.setLanguage(Locale.US); 
    } 
    if (status==TextToSpeech.LANG_MISSING_DATA){ 
     Toast.makeText(getApplicationContext(),"Text To Speech is not supported",Toast.LENGTH_LONG).show(); 
    } 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    tts.shutdown(); 
} 

private void SpeakOut() { 
String text=edt.getText().toString(); 
    if (null==text||"".equals(text)){ 
     text="Please give some input"; 
    } 
    tts.speak(text,TextToSpeech.QUEUE_FLUSH,null); 
} 
} 

我有一些話,他們看起來像這樣一個:能力(n。),或者這個:暈(adj。),但我不想TTS讀那個詞。有沒有辦法做到這一點?像:

if(text.contains("(n.)"){ 
    String newtext = text-"(n.)" 
} 

,然後告訴TTS閱讀newtext

回答

1
String stringToRemove = "(n.)"; 
if(text.contains(stringToRemove){ 
    String newtext = text.replaceAll(stringToRemove, ""); 
} 

希望它有效。

+0

哇!這工作!非常感謝天才。 –

+0

但是如果還有其他東西要刪除呢?像(adj。)或(exp。)...? –

+0

字符串stringToRemove = 「(調整後)」; //你可以在這裏更換任何文本刪除 如果(text.contains(stringToRemove){ 字符串newtext = text.replaceAll(stringToRemove, 「」);} 字符串 –

相關問題