2012年4月4日 星期三
Voice Recognition
res/layout/voice_recognition.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="4dip"
android:text="Click the button and start speaking" />
<Button
android:id="@+id/bVoice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="speakButtonClicked"
android:text="Click Me!" />
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
</ListView>
</LinearLayout>
***************************************************************
public class VoiceRecognitionDisplay extends Activity implements OnClickListener{
/** Called when the activity is first created. */
ListView listview;
static final int check = 987;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.voice_recognition);
listview = (ListView)findViewById(R.id.listView1);
Button button = (Button)findViewById(R.id.bVoice);
button.setOnClickListener(this);
}//end of onCreate
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent (RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak up son!");
startActivityForResult(intent, check);
}//end of onClick
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == check && resultCode == RESULT_OK){
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
//檢查一共幾筆資料
lv.setClickable(true);
Toast.makeText(Cq_VoiceRecognition02Activity.this, "一共 "+ Integer.toString(listview.getAdapter().getCount()) + " 個查詢到的項目", Toast.LENGTH_LONG).show();
//設定點選的項目
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// 取得Clicked String
String item = (String)listview.getAdapter().getItem(position);
//利用TOAST顯示點選的項目
Toast.makeText(Cq_VoiceRecognition02Activity.this, Integer.toString(position), Toast.LENGTH_SHORT).show();
Toast.makeText(Cq_VoiceRecognition02Activity.this, item, Toast.LENGTH_LONG).show();
}
});
}
super.onActivityResult(requestCode, resultCode, data);
}//end of OnActivityResult
}//end VoiceRecognitionDisplay
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言