1

私は簡単なゲームに取り組んでいます。最初のラウンドのアイデアは、最初のアクティビティに26個のボタン(画像)があるということです。英語のアルファベットで、ボタンをクリックしたときに発音させたいと思います。たとえば、「a」ボタンを押すと、「a」と表示されます。各ボタンのIDを取得し、その文字列を取得して、パラメーターとしてspeak()メソッドに指定するにはどうすればよいですか?前もって感謝します !

コードはここにあります:

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);

    }

}
4

2 に答える 2

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);

}
于 2012-06-10T17:35:31.160 に答える
0

onInit() メソッドは、ユーザーが言語データをインストールしていない場合の処理​​など、さらに多くのことを行う必要があります。ヘルパー クラスについては、このコードを参照してください。

于 2012-06-11T12:00:53.500 に答える