1),首先在官网下载相关jar包
2)自定义界面设计:
R.layout.activity_main.xml
main.xml
2) 逻辑实现
首先添加相关jar包以及so文件,如下
权限添加:
代码实现:
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(ArrayListresults, 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; }}