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
?
哇!這工作!非常感謝天才。 –
但是如果還有其他東西要刪除呢?像(adj。)或(exp。)...? –
字符串stringToRemove = 「(調整後)」; //你可以在這裏更換任何文本刪除 如果(text.contains(stringToRemove){ 字符串newtext = text.replaceAll(stringToRemove, 「」);} 字符串 –