2012-06-10 43 views
1

我正在做一个简单的游戏。第一轮的想法是,第一项活动 - 英文字母表上有26个按钮(图像),我希望在点击按钮时发音。例如,当我按下“a”按钮时,它必须说“a”。我怎样才能得到每个按钮的ID,然后它的字符串,并将它作为参数发言()方法?提前致谢 !TextToSpeech - Android

代码是在这里:

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.os.Vibrator; 
import android.widget.Button; 
import android.widget.TableLayout; 
import android.widget.TableRow; 

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


import android.speech.tts.TextToSpeech; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 

public class FirstActivity extends Activity { 

    /** Called when the activity is first created. */ 

    TextToSpeech ttx; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.firstact); 

     ttx = new TextToSpeech(
       FirstActivity.this, 
       new TextToSpeech.OnInitListener() { 

      public void onInit(int status) { 
       // TODO Auto-generated method stub 
       if(status !=TextToSpeech.ERROR){ 
        ttx.setLanguage(Locale.US);     
       } 

      } 
     }); 


     TableLayout table = (TableLayout) findViewById(R.id.table); 

     String alphabet = "abcdefghijqlmnopqrstuvwxyz"; 

     int pos = 0; 
     for (int i = 0; i < 7; i++) { 
      TableRow row = new TableRow(this); 
      for (int j = 0; j < 4; j++) { 
       if (pos == 26) { 
        break; 
       } 
       Button but = new Button(this); 
       /*but.setOnClickListener(btnListener);*/ 
       but.setText(Character.toString(alphabet.charAt(pos++))); 
       but.setHeight(80); 
       but.setWidth(70); 
       but.setBackgroundResource(R.drawable.burti); 
       row.addView(but); 
       but.setId(pos); 

       but.setOnClickListener((OnClickListener) this); 


      table.addView(row); 

     }} 



    } 
    @Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     if(ttx != null){ 
      ttx.stop(); 
      ttx.shutdown();  
     } 
     super.onPause(); 
    } 

    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 


     String pressed = but.getText().toString(); //error here.... 
     ttx.speak(pressed, TextToSpeech.QUEUE_FLUSH, null); 

    } 

} 

回答

1

我做了一个工作示例:http://frank.anemaet.nl/android/TalkingButtons.zip 这里的工作代码片段:

for (int i = 0; i < alphabet.length(); i++) 
    { 
     TableRow row = new TableRow(this); 
     Button but; 
     but = new Button(this); 
     but.setText(Character.toString(alphabet.charAt(i))); 
     but.setHeight(button_width); 
     but.setWidth(button_height); 
     but.setId(i); 

     but.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       performClick(v); 
      } 
      }); 

     row.addView(but); 
     table.addView(row); 
    } 

...

public void performClick(View arg0){ 

    Button tv = (Button)findViewById(arg0.getId()); 
    String pressed = tv.getText().toString(); 
    ttx.speak(pressed, TextToSpeech.QUEUE_FLUSH, null); 

} 
0

你onInit()方法必须做更多的事情,比如当用户没有安装语言数据时处理LED。 请参阅this code帮助类。