19 thg 5, 2011

Doc Doan Text trong EditText

Khi ban nhap vao button Play Random Tetx . Chuong trinh se doc doan van ban cua ban voi ngon ngu ma ban chon san. O day minh chon ngon ngu la English.
Do dung ubuntu chua co unikey nen viet khong dau. Mong cac ban thong cam.
Hinh cua chuong trinh :


 file main.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        />
    <Button
        android:id="@+id/generaterandom"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/generate_random_text"
        />
    <Button
        android:id="@+id/play"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/play_random_text"
        />
    <EditText
        android:id="@+id/beatbox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/android_random_beat_box"
        android:textSize="14dp"
        />       
</LinearLayout>

file AndroidBeatBox.java

package com.android.androidbeatbox;

import java.util.Locale;
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AndroidBeatBox extends Activity implements OnInitListener {
    final static int NUM_TEXT = 25 ;
    String[] beat = {"hehe ",
              "haha ",
              "kekeke ",
              "Sungha ",
              "CN08a ",
              "GTVT ",
           };
   
    Button buttonGenerateRandomText , buttonPlay ;
    EditText RandomBeatBox ;
   
    TextToSpeech tts ;
    Random random ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        buttonGenerateRandomText = (Button) findViewById(R.id.generaterandom);
        buttonPlay = (Button) findViewById(R.id.play);
        buttonPlay.setEnabled(false);
       
        RandomBeatBox = (EditText) findViewById(R.id.beatbox);
       
        random = new Random();
        tts = new TextToSpeech(this, this);
        tts.setLanguage(Locale.ENGLISH);
       
        //Tao su kien khi click vao nut Generate Random Text
        buttonGenerateRandomText.setOnClickListener(buttonGenerateRandomTextOnClick);
        buttonPlay.setOnClickListener(buttonPlayOnClick);
           }
        // Su kien buttonGenerateRandomTextOnClick cho nut Generate Random Text
        private Button.OnClickListener buttonGenerateRandomTextOnClick = new Button.OnClickListener()
        {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String strBeat = "" ;
                for(int i=0;i<= NUM_TEXT;i++)
                {
                    int randomInt = random.nextInt(beat.length);
                    strBeat = strBeat + beat[randomInt];
                }
                RandomBeatBox.setText(strBeat);
               
            }
           
        };
       
        private Button.OnClickListener buttonPlayOnClick = new Button.OnClickListener()
        {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                tts.speak(RandomBeatBox.getText().toString(),TextToSpeech.QUEUE_ADD ,null );
            }
           
        };
       
    public void onInit(int arg0) {
        // TODO Auto-generated method stub
        buttonPlay.setEnabled(true);
    }
    @Override
    public void onDestroy(){
        super.onDestroy();
        tts.shutdown();
    }
}


bai viet kham khao tren android.forum. Than mem. !!!














Không có nhận xét nào:

Đăng nhận xét

Bản beta đầu tiên

Sau 6 tháng cả team cặm cụi làm việc điên cuồng, bản alpha cũng được giới thiệu ra toàn bộ công ty và được testing nội bộ công ty mà thôi. ...