2016-10-14 9 views
0

私は新しいアンドロイドデベロッパーです。私は、2つのedittextとSpeech To Text機能を備えたボタンごとにアクティビティを作成したいと思います。私はちょうどコードを見つけました。しかし、私は2つのボタンと2つのedittextのためにそれを解くことができません。アドバイスありがとう。2ボタンと2の編集テキストのテキスト(アンドロイド)

package info.androidhive.speechtotext; 

import java.util.ArrayList; 
import java.util.Locale; 

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 { 

private EditText txtSpeechInput1, txtSpeechInput2; 
private Button btnSpeak1, btnSpeak2; 
private final int REQ_CODE_SPEECH_INPUT = 100; 

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

    txtSpeechInput1 = (EditText) findViewById(R.id.txtSpeechInput1); 
    txtSpeechInput1 = (EditText) findViewById(R.id.txtSpeechInput2); 
    btnSpeak1 = (Button) findViewById(R.id.btnSpeak1); 
    btnSpeak2 = (Button) findViewById(R.id.btnSpeak2); 

    // hide the action bar 
    getActionBar().hide(); 

    btnSpeak1.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      promptSpeechInput(); 
     } 
    }); 

    btnSpeak2.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      promptSpeechInput(); 
     } 
    }); 

} 

/** 
* Showing google speech input dialog 
* */ 
private void promptSpeechInput() { 
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
      RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); 
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, 
      getString(R.string.speech_prompt)); 
    try { 
     startActivityForResult(intent, REQ_CODE_SPEECH_INPUT); 
    } catch (ActivityNotFoundException a) { 
     Toast.makeText(getApplicationContext(), 
       getString(R.string.speech_not_supported), 
       Toast.LENGTH_SHORT).show(); 
    } 
} 

/** 
* Receiving speech input 
* */ 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

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

      ArrayList<String> result = data 
        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
      txtSpeechInput1.setText(result.get(0)); 
     } 
     break; 
    } 

    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 

答えて

0

これは

package info.androidhive.speechtotext; 

import java.util.ArrayList; 
import java.util.Locale; 

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 { 

private EditText txtSpeechInput1, txtSpeechInput2; 
private Button btnSpeak1, btnSpeak2; 
private final int REQ_CODE_SPEECH_INPUT1 = 101; 
private final int REQ_CODE_SPEECH_INPUT2 = 102; 

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

    txtSpeechInput1 = (EditText) findViewById(R.id.txtSpeechInput1); 
    txtSpeechInput2 = (EditText) findViewById(R.id.txtSpeechInput2); 
    btnSpeak1 = (Button) findViewById(R.id.btnSpeak1); 
    btnSpeak2 = (Button) findViewById(R.id.btnSpeak2); 

    // hide the action bar 
    getActionBar().hide(); 

    btnSpeak1.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      promptSpeechInput(REQ_CODE_SPEECH_INPUT1); 
     } 
    }); 

    btnSpeak2.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      promptSpeechInput(REQ_CODE_SPEECH_INPUT2); 
     } 
    }); 

} 

/** 
* Showing google speech input dialog 
* */ 
private void promptSpeechInput(int req) { 
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
      RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); 
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, 
      getString(R.string.speech_prompt)); 
    try { 
     startActivityForResult(intent, req); 
    } catch (ActivityNotFoundException a) { 
     Toast.makeText(getApplicationContext(), 
       getString(R.string.speech_not_supported), 
       Toast.LENGTH_SHORT).show(); 
    } 
} 

/** 
* Receiving speech input 
* */ 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

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

      ArrayList<String> result = data 
        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
      txtSpeechInput1.setText(result.get(0)); 
     } 
     break; 
    case REQ_CODE_SPEECH_INPUT2: { 
     if (resultCode == RESULT_OK && null != data) { 

      ArrayList<String> result = data 
        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
      txtSpeechInput2.setText(result.get(0)); 
     } 
     break; 

    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 
android 
+0

は、それは非常にうまく機能、どうもありがとうございまし動作するはずです。 "protected void onActivityResult"の最初の "break"の後ろに括弧(})を追加するだけです。 –

関連する問題