删除android中的一部分字符串

时间:2016-02-08 13:31:04

标签: android text-to-speech

我有一个应用程序,我有TextToSpeech功能来阅读编辑文本中的大量单词,这是我的代码:

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);
}
}

我有一些他们看起来像这样的话:ability(n。),或者这个:dizzy(adj。),但我不希望TTS读出那个词的那种。有没有办法做到这一点?喜欢:

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

然后告诉TTS阅读newtext

1 个答案:

答案 0 :(得分:1)

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

希望它有效。