2017-05-06 13 views

答えて

-1

Googleの機能であるText-to-Speechを使用する必要があります。

public class MainActivity extends AppCompatActivity { 
    TextToSpeech t1; 
    EditText ed1; 
    Button b1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ed1=(EditText)findViewById(R.id.editText); 
     b1=(Button)findViewById(R.id.button); 

     t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { 
     @Override 
     public void onInit(int status) { 
      if(status != TextToSpeech.ERROR) { 
       t1.setLanguage(Locale.UK); 
      } 
     } 
     }); 

     b1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String toSpeak = ed1.getText().toString(); 
      Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show(); 
      t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null); 
     } 
     }); 
    } 

    public void onPause(){ 
     if(t1 !=null){ 
     t1.stop(); 
     t1.shutdown(); 
     } 
     super.onPause(); 
    } 
}