语音翻译手语

时间:2013-12-04 06:59:09

标签: android

我在我的程序中使用了两个活动。在第一个活动中,我完成了语音到文本的编码,在第二个活动中,我完成了文本编码以签署语言,我通过按钮链接它。但它不会移动到另一个活动。我希望这可以在另一项活动中显示语音识别的文本。请帮助

第一个语音识别器的java代码

package com.authorwjf.talk2me;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    protected static final int REQUEST_OK = 1;
    public static String EXTRA_MESSAGE;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button1).setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
         Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
         i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
         try {
             startActivityForResult(i, REQUEST_OK);
         } catch (Exception e) {
             Toast.makeText(this, "Error initializing speech to text engine.", Toast.LENGTH_LONG).show();
         }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==REQUEST_OK  && resultCode==RESULT_OK) {
            ArrayList<String> thingsYouSaid = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            ((TextView)findViewById(R.id.text1)).setText(thingsYouSaid.get(0));
        }
    }
    public void sendMessage(View view) {
        // Do something in response to button
            Intent intent = new Intent(MainActivity.this, Second.class);
            EditText editText = (EditText) findViewById(R.id.text1);
            String message = editText.getText().toString();
            intent.putExtra(EXTRA_MESSAGE, message);
            startActivity(intent);
    }


}

这是xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="132dp"
        android:text="..." />

    <ImageButton
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text1"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:layout_marginTop="37dp"
        android:src="@android:drawable/ic_btn_speak_now" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/text1"
        android:layout_alignBottom="@+id/text1"
        android:layout_alignLeft="@+id/button1"
        android:text="Enter" />

</RelativeLayout>

这是我的第二项活动

package com.authorwjf.talk2me;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint("NewApi")
public class Second extends Activity {

    // View Objects
        Button translate;
        EditText enterText;
        TextView displayText;
        ImageView aslImages;
        MediaPlayer mP;
        Second thisClass;
        // Variables for Translation and Display
        int phraseIndex = 0; // Keep track of the array indexes
        String letters; // Message to be Translated
        String display; // Will hold the letters already displayed and show them
        String PhraseIndex;
        // Array Libraries For Characters and Image References
        String letterIndex[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
                "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
                "x", "y", "z", "hello", "hi", "happy", "sad", "dad", "mom", "school", " " };

        int aslPics[] = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
                R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h,
                R.drawable.i, R.drawable.j, R.drawable.k, R.drawable.l,
                R.drawable.m, R.drawable.n, R.drawable.o, R.drawable.p,
                R.drawable.q, R.drawable.r, R.drawable.s, R.drawable.t,
                R.drawable.u, R.drawable.v, R.drawable.w, R.drawable.x,
                R.drawable.y, R.drawable.z, R.drawable.hello, R.drawable.happy, R.drawable.sad,
                R.drawable.dad, R.drawable.mom, R.drawable.school, R.drawable.space };

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            thisClass = this;
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // Show the Up button in the action bar.
            setupActionBar();
             // Get the message from the intent
            Intent intent = getIntent();
            String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

            // Create the text view
            TextView textView = new TextView(this);
            textView.setTextSize(40);
            textView.setText(message);

            // Set the text view as the activity layout
            setContentView(textView);

            // Attach objects to view objects
            translate = (Button) findViewById(R.id.buttonTranslate);
            translate.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    setString(v);
                    translateLetter();
                }
            });
            enterText = (EditText) findViewById(R.id.textInput);
            displayText = (TextView) findViewById(R.id.displayText);
            aslImages = (ImageView) findViewById(R.id.aslViewer);
            aslImages.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    translateLetter();
                }
            });

            // Select all the Text in the enterText Field
            enterText.setSelectAllOnFocus(true);
        }

        // setString Function
        // Acts when the Translate Button is pressed
        public void setString(View v) {
            // Set the text of displayText to'--- '"
            displayText.setText("   ----");


            // Reset the Phrase Index
            phraseIndex = 0;
            display = "";

            // Get the Input Text
            String phrase = enterText.getText().toString();
            letters = phrase.toLowerCase();
        }

        // Displays Image when ImageView Touched.
        // Also displays text of letters already translated

        public void translateLetter() {
            // Checks if letters string is null-displays message
            // If the phrase has not been converted to a string
            if (letters == null) {
                displayText.setText("Press the Translate Button ");
            }

            // Checks if letters string is null-will not display
            // ASL letters until translate Button is pressed
            if (letters != null) {
                // Fetch the current Character in the phrase
                String currentLetter = letters;

                // add the letter to the display string
                display += currentLetter;

                // Search for the corresponding ASL image by Index
                for (int i = 0; i < letterIndex.length; i++) {
                    if (letterIndex[i].contentEquals(currentLetter)) {
                        // Display the image
                        aslImages.setImageResource(aslPics[i]);
                    } // end if
                }// end for
                    // Set the text to display the letters translated
                displayText.setText("   " + display);
                // Advance to the Next Letter in the Phrase
                phraseIndex++;

                // Check to see if you reach the end of the phrase
                if (phraseIndex > letters.length() - 1) {
                    // Reset back to the first character
                    phraseIndex = 0;
                    display = "";
                } // end if
            }// end Function translateLetter()
        }// end if

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
        /**
         * Set up the {@link android.app.ActionBar}.
         */
        private void setupActionBar() {

            getActionBar().setDisplayHomeAsUpEnabled(true);

        }



        @SuppressLint("NewApi")
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:
                // This ID represents the Home or Up button. In the case of this
                // activity, the Up button is shown. Use NavUtils to allow users
                // to navigate up one level in the application structure. For
                // more details, see the Navigation pattern on Android Design:
                //
                // http://developer.android.com/design/patterns/navigation.html#up-vs-back
                //
                NavUtils.navigateUpFromSameTask(this);
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

}

这是第二个xml文件

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TableRow 
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/aslViewer"
            android:layout_width="wrap_content"
            android:layout_height="300dp"
            android:layout_span="2"
            android:onClick="translateletter"
            android:contentDescription="@string/hand_images"
            android:src="@drawable/a" />

    </TableRow>

<TableRow
    android:id="@+id/tableRow3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/displayText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_span="2"
        android:text="@string/T_text"/>
    </TableRow>

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/buttonTranslate"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
       android:onClick="setString"
       android:text="@string/Translate"/>

    <EditText
        android:id="@+id/textInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/Enter_Text" >

    </EditText>

</TableRow>


</TableLayout>

这个清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.authorwjf.talk2me"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.authorwjf.talk2me.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity
            android:name="com.authorwjf.talk2me.Second"
            android:label="@string/title_activity_display_message"
            android:parentActivityName="com.authorwjf.talk2me.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.authorwjf.talk2me.MainActivity" />
        </activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

我想你忘了在点击按钮时拨打sendMessage(View view)。 为button2添加android:onClick="sendMessage"

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/text1"
    android:layout_alignBottom="@+id/text1"
    android:layout_alignLeft="@+id/button1"
    android:text="Enter"
    android:onClick="sendMessage" />

另外,对于button1的安全检查,

@Override
public void onClick(View v) {
    if(v.getId()==R.id.button1){
        //Your code
     }
}

在第二项活动中,我不认为使用setContentView(textView);是正确的。在此行之后,您可能会收到NullPointerException。我认为'吐司'就足够了。或者在xml中声明TextView而不是动态创建。