[.xml] ImageButton과 EditText를 넣어줍니다. Button도 상관 없습니다.
<EditText
android:id="@+id/editText"
android:layout_width = "match parent"
android:layout_height = "match parent" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="43dp"
android:layout_height="44dp" />
[.java]
public class MainActivity extends AppCompatActivity {
private TextToSpeech sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText = (EditText) findViewById(R.id.editText);
ImageButton imageButton= (ImageButton) findViewById(R.id.imageButton)
sp = new TextToSpeech(this, new TextToSpeech.OnInitListener() { // TextToSpeech함수를 참조해줍니다.
@Override
public void onInit(int i) {
if(i == TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.KOREAN);
}
}
});
imagebutton.setOnClickListener(new View.OnClickListener() { // editText의 문자열을 음성으로 읽는 함수
@Override
public void onClick(View view) {
sp.speak(editText.getText().toString(),TextToSpeech.QUEUE_FLUSH, null);
}
});