如何在具有多个LinearLayout的textView上使用TextToSpeech?
我'试图在电子邮件上添加TTS方法来阅读它,所以我从开源处工作邮件客户端并开始在其中添加TTS代码,所以当点击按钮时,应该大声读出消息
这就是我的所作所为:
活动类的名称是MessageContainerView
unsignedText = (TextView) findViewById(R.id.message_unsigned_text);
buttonRead =(Button)findViewById(R.id.button);
t1=new TextToSpeech(activity.getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
t1.setLanguage(Locale.UK);
}
}
});
buttonRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String toSpeak = unsignedText.getText().toString();
Toast.makeText(activity.getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show();
t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
});
但是当我点击按钮时,应用停止了
<?xml version="1.0" encoding="utf-8"?>
<com.fsck.k9.ui.messageview.MessageContainerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Content area -->
<com.fsck.k9.view.MessageWebView
android:id="@+id/message_content"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
<!-- Unsigned content area -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/message_unsigned_container"
android:visibility="gone"
tools:visibility="visible"
>
<LinearLayout
android:id="@+id/message_unsigned_divider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
>
<View
android:layout_width="16dp"
android:layout_height="4dp"
android:layout_marginTop="1dp"
android:layout_gravity="center_vertical"
android:background="#d55"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:textAppearance="?android:textAppearanceSmall"
android:text="@string/unsigned_text_divider_label"
android:textColor="#d55"
/>
<View
android:layout_width="wrap_content"
android:layout_height="4dp"
android:layout_marginTop="1dp"
android:layout_gravity="center_vertical"
android:background="#d55" />
</LinearLayout>
<TextView
android:id="@+id/message_unsigned_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="8dp"
tools:text="Unsigned text content"
/>
</LinearLayout>
<!-- Attachments area -->
<LinearLayout
android:id="@+id/attachments_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/attachments"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</com.fsck.k9.ui.messageview.MessageContainerView>
&#13;
这是因为多个LinearLayout?所以TTS代码无法访问TextView中的文本?
答案 0 :(得分:0)
文本到语音逐个多个文本。这个
private void speakOut () {
String que = tvone.getText().toString();
String op1 = tvtwo.getText().toString();
String op2 = tvthree.getText().toString();
String op3 = tvfoure.getText().toString();
String op4 = tvfive.getText().toString();
tts.speak(que, TextToSpeech.QUEUE_ADD, null);
tts.speak(op1, TextToSpeech.QUEUE_ADD, null);
tts.speak(op2, TextToSpeech.QUEUE_ADD, null);
tts.speak(op3, TextToSpeech.QUEUE_ADD, null);
tts.speak(op4, TextToSpeech.QUEUE_ADD, null);
}