1

以下のコードは、Android アプリで音声をテキストに変換するために使用されます。このコードは、音声ボタンが押されたときに機能し、あなたの発言がテキストに変換されます。音声機能が閉じると、テキストが表示されます。スピーチ ボタンをもう一度押すと、前のテキストがクリアされ、新しいテキストがページに表示されます。私がしたいのは、話すボタンをクリックするたびに、ページに単語を表示したい..次に、もう一度話すボタンをクリックすると、次の単語がリスト形式の最初の単語の下に表示されるようにすることです. リストビューを使用しようとしていますが、苦労しています。ここに元のコードと、リストビューにするために記述しようとしているコードがあります。誰でも助けることができますか?

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_toLeftOf="@+id/textView1"
android:gravity="center"
android:orientation="vertical" >

<ImageButton
    android:id="@+id/btnSpeak"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:contentDescription="@string/speak"
    android:src="@android:drawable/ic_btn_speak_now" />

<TextView
    android:id="@+id/txtText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />



</LinearLayout>

コード:

import java.util.ArrayList;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

protected static final int RESULT_SPEECH = 1;

private ImageButton btnSpeak;
private TextView txtText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtText = (TextView) findViewById(R.id.txtText);

    btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

    btnSpeak.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent = new Intent(
                    RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

            try {
                startActivityForResult(intent, RESULT_SPEECH);
                txtText.setText("");
            } catch (ActivityNotFoundException a) {
                Toast t = Toast.makeText(getApplicationContext(),
                        "Ops! Your device doesn't support Speech to Text",
                        Toast.LENGTH_SHORT);
                t.show();
            }
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case RESULT_SPEECH: {
        if (resultCode == RESULT_OK && null != data) {

            ArrayList<String> text = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

            txtText.setText(text.get(0));
        }
        break;
    }

    }
}
}
4

1 に答える 1

0

onActivityResult では、TextView を最初の単語のみ (インデックス 0) でオーバーライドしています。次のように String 配列リストを反復処理する必要があります。

    String words = "";
    for ( String word : text ) {
        words += " " + word;
    }
    txtText.setText(words);

そして、ListView の実装に役立つ素晴らしい記事があります: ListView チュートリアル

それで、それを理解したら、リストビューに値という単語を追加します。

これが役に立てば幸いです、乾杯

于 2013-01-09T19:02:46.313 に答える