1),首先在官网下载相关jar包
2)自定义界面设计:
R.layout.activity_main.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" android:padding="10dp" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请先到餐厅预定选择店铺" android:textAppearance="?android:attr/textAppearanceMedium" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageButton android:id="@+searchdialog/imageButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/transparent" android:padding="10dp" android:src="@android:drawable/ic_btn_speak_now" /> <EditText android:id="@+searchdialog/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:padding="10dp" > <requestFocus /> </EditText> </LinearLayout> </LinearLayout>
main.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" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/content" /> </LinearLayout>
2) 逻辑实现
首先添加相关jar包以及so文件,如下
权限添加:
<uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
代码实现:
package com.wangfubin.xunfenyuyin; import java.util.ArrayList; import com.iflytek.speech.RecognizerResult; import com.iflytek.speech.SpeechError; import com.iflytek.ui.RecognizerDialog; import com.iflytek.ui.RecognizerDialogListener; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.Toast; public class MainActivity extends Activity { private EditText setedit; private ImageButton yuyinButton; private Button button1; private String textString = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { LinearLayout LinearLayout1 = (LinearLayout) getLayoutInflater() .inflate(R.layout.activity_main, null); setedit = (EditText) LinearLayout1.findViewById(R.searchdialog.editText1); yuyinButton = (ImageButton) LinearLayout1.findViewById(R.searchdialog.imageButton1); new AlertDialog.Builder(MainActivity.this) .setTitle("请输入查询关键字") .setIcon(android.R.drawable.ic_dialog_info) .setView(LinearLayout1) .setPositiveButton("取消", null) .setNegativeButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { /*// 数据获取 searchtext(setedit.getText().toString());*/ Toast.makeText(MainActivity.this, setedit.getText().toString(), Toast.LENGTH_LONG).show(); } }).show(); yuyinButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(1); } }); } }); } /** * 语音查询 */ @Override protected Dialog onCreateDialog(int id) { RecognizerDialog recognizerDialog = new RecognizerDialog( MainActivity.this, "appid=5132fe14");// 这里应该写从科大讯飞申请到的appid recognizerDialog.setEngine("sms", null, null); recognizerDialog.setListener(new RecognizerDialogListener() { @Override public void onResults(ArrayList<RecognizerResult> results, boolean arg1) { for (int i = 0; i < results.size(); i++) { textString += results.get(i).text; } } @Override public void onEnd(SpeechError arg0) { // Toast.makeText(MainActivity.this, textString, 1).show(); setedit.setText(textString.substring(0, textString.length() - 1));
textString =""; } }); return recognizerDialog; } }