解決したいこと
今、音声認識のアプリをAndroidStudioで作成中です。
イメージとしては、音声認識のボタンを押すとマイクのマークが表示され、アンダーバーで引かれた箇所に音声認識をした文字がテキストで表示される。(本来は、そこで送信ボタンを押すとテキストがもう一方のスマホ(ゆくゆくはスマートグラスの予定)に送信される。という機能を目指している。)
といったアプリですが、参考にしている本がクラスしか提供しておらず、
色々と試行錯誤しながらレイアウトエディタを作ってみましたが、思い通りの実行に至りませんでしたので、どなたか音声認識アプリを使った方含めて、アドバイスをいただきたいです。
・レイアウトエディタは3こ作らなければならないのですか?
・レイアウトエディタには何のウィジェットを用いれば良いのでしょうか?
・引数など何が何に対応しているか(例:音声認識ボタンのIDなど)さっぱり分かりません。
・自分っで作ることは何なのか。
1つでもかまいませんので教えていただけると幸いです。
RecognizeSpeechExクラス(本により提供されているクラス)
Java
1import android.app.Activity; 2import android.content.ActivityNotFoundException; 3import android.content.Intent; 4import android.graphics.Color; 5import android.os.Bundle; 6import android.speech.RecognizerIntent; 7import android.view.View; 8import android.view.ViewGroup; 9import android.view.Window; 10import android.widget.Button; 11import android.widget.EditText; 12import android.widget.LinearLayout; 13import android.widget.Toast; 14 15import java.util.ArrayList; 16 17//音声認識 18public class RecognizeSpeechEx extends Activity 19 implements View.OnClickListener { 20 private final static int WC = ViewGroup.LayoutParams.WRAP_CONTENT; 21 private final static int MP = ViewGroup.LayoutParams.MATCH_PARENT; 22 private static final int REQUEST_CODE = 0; 23 private EditText editText; 24 25 //アクティビティ起動時に呼ばれる 26 @Override 27 public void onCreate(Bundle bundle) { 28 super.onCreate(bundle); 29 requestWindowFeature(Window.FEATURE_NO_TITLE); 30 31 //レイアウトの作成 32 LinearLayout layout = new LinearLayout(this); 33 layout.setBackgroundColor(Color.WHITE); 34 layout.setOrientation(LinearLayout.VERTICAL); 35 setContentView(layout); 36 37 //エディットテキストの生成 38 editText = new EditText(this); 39 editText.setText(""); 40 editText.setLayoutParams(new LinearLayout.LayoutParams(MP, WC)); 41 layout.addView(editText); 42 43 //ボタンの生成 44 layout.addView(makeButton("音声認識", "recog")); 45 } 46 47 //ボタンの生成 48 private Button makeButton(String text, String tag) { 49 Button button = new Button(this); 50 button.setText(text); 51 button.setTag(tag); 52 button.setOnClickListener(this); 53 button.setLayoutParams(new LinearLayout.LayoutParams(WC, WC)); 54 return button; 55 } 56 57 //ボタンクリック時に呼ばれる 58 public void onClick(View v) { 59 try { 60 //音声認識の実行 61 Intent intent = new Intent( 62 RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 63 intent.putExtra( 64 RecognizerIntent.EXTRA_LANGUAGE_MODEL, 65 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 66 intent.putExtra( 67 RecognizerIntent.EXTRA_PROMPT, 68 "RecognizeSPeechEX"); 69 startActivityForResult(intent, REQUEST_CODE); 70 } catch (ActivityNotFoundException e) { 71 toast(e.getMessage()); 72 } 73 } 74 75 //アクティビティ終了時に呼ばれる 76 protected void onActivityResult(int requestCode, 77 int resultCode, Intent data) { 78 //音声認識結果の取得 79 if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) { 80 String str = ""; 81 ArrayList<String> results = 82 data.getStringArrayListExtra( 83 RecognizerIntent.EXTRA_RESULTS); 84 for (int i = 0; i < results.size(); i++) { 85 str += results.get(i) + " "; 86 } 87 editText.setText(str); 88 } 89 super.onActivityResult(requestCode, resultCode, data); 90 } 91 92 //トーストの表示 93 private void toast(String text) { 94 Toast.makeText(this, text, Toast.LENGTH_LONG).show(); 95 } 96}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/13 02:45