5분 안에 간단한 TextToSpeech 유틸리티 앱 만들기
TextToSpeech.OnInitListener
다음과 같이 보일 것입니다.
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener
activity_main
의 코드 아래에 다음 코드 붙여넣기를 사용하여 표지 디자인을 만듭니다.<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/txt_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="@string/text_to_speech"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/et_input_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:background="@drawable/rectangle"
android:ems="10"
android:hint="@string/enter_some_text"
android:inputType="textPersonName"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_label" />
<Button
android:id="@+id/btn_speak_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="@string/speak_now"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_input_text" />
</androidx.constraintlayout.widget.ConstraintLayout>
strings.xml
<string name="app_name">CodeWithRish</string>
<string name="text_to_speech">Text To Speech</string>
<string name="enter_some_text">Enter Some Text ......</string>
<string name="speak_now">Speak Now</string>
rectangle.xml
을 생성하고 코드 아래에 붙여넣기<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
<stroke android:width="2dp" android:color="#03A9F4" />
<padding android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" />
<corners android:radius="5dp" />
<solid android:color="#ffffffff" />
</shape>
MainActivity.java
를 열고 코드를 다음으로 바꿉니다.import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Locale;
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
private TextToSpeech textToSpeech;
private EditText etInputText;
private Button btnSpeakText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
btnSpeakText.setOnClickListener(click -> {
speakText();
});
}
private void speakText() {
String input = etInputText.getText().toString();
textToSpeech.speak(input, TextToSpeech.QUEUE_FLUSH, null);
}
private void init() {
textToSpeech = new TextToSpeech(this, this);
etInputText = findViewById(R.id.et_input_text);
btnSpeakText = findViewById(R.id.btn_speak_text);
}
@Override
public void onInit(int state) {
if (state == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.UK);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(this, "Language Not Supported", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Enter Text", Toast.LENGTH_SHORT).show();
}
}
}
}
이제 동일한 색상을 원하면
colorPrimary
및 colorPrimaryVariant
를 대체할 수 있습니다.#FFEB3B
색상 코드가 있는 색상.
Reference
이 문제에 관하여(5분 안에 간단한 TextToSpeech 유틸리티 앱 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/codewithrish/create-simple-texttospeech-utility-app-in-5-minutes-2bad텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)