?
?é é¢å¦‚下:
?
?
?
?
?
?
主é é¢çš„布局分隔為三部分:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gztab_content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation=" vertical " > <!-- header --> <include android:id="@+id/gz_top" layout="@layout/gzmgr_top_panel" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <!-- content --> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TabWidget android:id ="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" > </TabWidget> <FrameLayout android:id ="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id ="@+id/unhanlderLayout "> <Button android:id="@+id/gzbtn_unhandler_refresh" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="刷新" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/gz_unhandler_ListView" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id ="@+id/handlingLayout"> <Button android:id="@+id/gzbtn_handlering_refresh" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="刷新" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/gz_handleringListView" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id ="@+id/handledLayout"> <Button android:id="@+id/gzbtn_handled_refresh" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="刷新" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/gzgzbtn_handled_ListView" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> <!-- bottom --> <Button android:id="@+id/btnManyInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="更多(10)"/> </LinearLayout>
?
注æ„觀察上é¢æ¨™è¨˜ç‚ºç´…色的android:idå‡é‡‡ç”¨android系統(tÇ’ng)默èªçš„å稱:
?
é é¢çš„å°Ž(dÇŽo)航組件:
<?xml version="1.0" encoding="UTF-8"?> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:id="@+id/top_gzbtn_left" android:text="返回" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="12.0dip" android:layout_alignParentLeft="true" android:layout_centerVertical="true" /> <Button android:id="@+id/top_gzbtn_right" android:text="發(fÄ)布" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="12.0dip" android:layout_alignParentRight="true " android:layout_centerVertical="true " /> <TextView android:textSize="22.0sp" android:textColor="#ffffffff" android:ellipsize="middle" android:gravity="center_horizontal" android:id="@+id/top_gztxt_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="æ•…éšœç®¡ç† " android:singleLine="true" android:layout_toLeftOf="@+id/top_gzbtn_right" android:layout_toRightOf="@+id/top_btn_left0" android:layout_centerInParent="true" android:layout_alignWithParentIfMissing="true" > </TextView> </RelativeLayout>
?
上é¢ç´…色布局主è¦é‡‡ç”¨ç›¸å°å¸ƒå±€å®šä½ç›¸é—œ(guÄn)çš„å°Ž(dÇŽo)航ä½ç½®ï¼š
?
?
æ¯ä¸€å€‹tabé é¢çš„ä¸çš„å…§(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- å€‹äººä¿¡æ¯ --> <LinearLayout android:id="@+id/gzPersonInfoLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <ImageView android:id="@+id/gzimg_item_Img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px" android:layout_gravity="left" android:src="@drawable/ic_launcher"/> <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:shrinkColumns="1" android:layout_gravity="right" > <TableRow > <TextView android:id="@+id/gztxt_item_userCode" android:padding="3dip" /> <TextView android:id="@+id/gztxt_item_UserCollectionName" android:padding="3dip" /> <TextView android:id="@+id/gztxt_item_Time" android:padding="3dip" /> </TableRow> <TableRow > <TextView android:id="@+id/gztxt_item_name_person" android:padding="3dip" /> <TextView android:text="" android:padding="3dip" /> <TextView android:padding="3dip" /> </TableRow> </TableLayout> </LinearLayout> <LinearLayout android:id="@+id/gzItemLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableRow> <TextView android:text="é …ç›®å稱:" android:padding="3dip" /> <TextView android:id="@+id/gztxt_item_name" android:padding="3dip" /> <TextView android:text="列車編號:" android:padding="3dip" /> <TextView android:id="@+id/gztxt_item_code" android:padding="3dip" /> </TableRow> </TableLayout> </LinearLayout> <LinearLayout android:id="@+id/gzItemLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:shrinkColumns="1" android:layout_gravity="right" > <TableRow > <TextView android:text="æ•…éšœè² è²¬(zé)人" android:padding="3dip" /> <TextView android:id="@+id/gztxt_item_user" android:padding="3dip" /> </TableRow> <TableRow > <TextView android:text="故障處ç†äºº" android:padding="3dip" /> <TextView android:id="@+id/gztxt_item_handler" android:padding="3dip" /> </TableRow> <TableRow > <TextView android:text="æ•…éšœæè¿°" android:padding="3dip" /> <TextView android:id="@+id/gztxt_item_descption" android:padding="3dip" /> </TableRow> <TableRow > <TextView android:text="å•é¡Œç´šåˆ¥ï¼š" android:padding="3dip" /> <TextView android:id="@+id/gztxt_item_level" android:text="一般" android:padding="3dip" /> </TableRow> <TableRow > <TextView android:text="狀態(tà i):" android:padding="3dip" /> <TextView android:id="@+id/gztxt_item_status" android:text="未解決" android:padding="3dip" /> </TableRow> </TableLayout> </LinearLayout> </LinearLayout>
?
?
?
?
訊飛的語音sdk是需è¦ç”³è«‹çš„,地å€æ˜¯ï¼š http://dev.voicecloud.cn/developer.php?vt=1 ?。申請一個訊飛的開發(fÄ)者賬號,然åŽç”³è«‹ä¸€å€‹appid,申請的時候需è¦å¡«å¯«é–‹ç™¼(fÄ)者信æ¯å’Œä½ 的應(yÄ«ng)用的信æ¯ã€‚
申請之åŽç¶“(jÄ«ng)éŽå¯©æ ¸é€šéŽï¼Œæœƒå¾—到一個appid,這個是在使用語音æœå‹™(wù)時需è¦ç”¨åˆ°çš„。(ç†è€…感覺申請還是比較容易通éŽçš„,簡單地填寫一下就通éŽäº†ã€‚速度也很快,我在晚上å一點多申請的,到第二天早上ä¹é»žå¤šå°±æ”¶åˆ°å¯©æ ¸é€šéŽçš„éƒµä»¶ã€‚å€‹äººæ„Ÿè¦ºé€™å€‹å¯©æ ¸åªæ˜¯ç‚ºäº†é˜²æ¢æƒ¡æ„使用,畢竟語音æœå‹™(wù)是è¦ä½¿ç”¨è¨Šé£›çš„æœå‹™(wù)器資æºçš„。)
申請到appid之åŽå°±å¯ä»¥ä¸‹è¼‰SDK了,然åŽä½¿ç”¨èªžéŸ³æœå‹™(wù)了。
以下我試用的éŽç¨‹ï¼Œï¼ˆé»žæ“Šbutton,彈出語音框,說完之åŽå°‡è˜åˆ¥çš„çµ(jié)果顯示在EditTextä¸ ï¼‰ï¼š
?
- 在eclipse里新建一個android工程
- 導(dǎo)入訊飛的語音jar包
-
訊飛的æœå‹™(wù)是需è¦ä¸€å †æ¬Š(quán)é™çš„,在manifest.xmlä¸åŠ å…¥
- < 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" ? /> ??
<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" />
分別為:通éŽéº¥å…‹é¢¨(fÄ“ng)錄音ã€è¯(lián)網(wÇŽng)ã€ç²å–網(wÇŽng)絡(luò)ä¿¡æ¯ç‹€æ…‹(tà i)ã€ç²å–wifi狀態(tà i)ã€æ”¹è®Šç¶²(wÇŽng)絡(luò)狀態(tà i)如是å¦èƒ½è¯(lián)網(wÇŽng)ã€è¨ªå•é›»è©±ç‹€æ…‹(tà i) -
在main.xmlä¸æ·»åŠ 一個EditText和一個Button,如下
- <? xml ? version = "1.0" ? encoding = "utf-8" ?> ??
- < LinearLayout ? xmlns:android = "http://schemas.android.com/apk/res/android" ??
- ???? android:layout_width = "fill_parent" ??
- ???? android:layout_height = "fill_parent" ??
- ???? android:orientation = "vertical" ? > ??
- ??
- ???? < EditText ??
- ???????? android:id = "@+id/editText" ??
- ???????? android:layout_width = "fill_parent" ??
- ???????? android:layout_height = "300dp" ??
- ???????? android:gravity = "top" ??
- ???????? android:inputType = "textMultiLine" ? > ??
- ??
- ???????? < requestFocus ? /> ??
- ???? </ EditText > ??
- ??
- ???? < Button ??
- ???????? android:id = "@+id/button_start" ??
- ???????? android:layout_width = "wrap_content" ??
- ???????? android:layout_height = "wrap_content" ??
- ???????? android:text = "點擊開始說話" ? /> ??
- ??
- </ LinearLayout > ??
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/editText" android:layout_width="fill_parent" android:layout_height="300dp" android:gravity="top" android:inputType="textMultiLine" > <requestFocus /> </EditText> <Button android:id="@+id/button_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="點擊開始說話" /> </LinearLayout>
- 然åŽåœ¨MainActivityä¸ç·¨å¯«ä»£ç¢¼å¯¦ç¾(xià n)了
?
(這里å¯ä»¥çœ‹åˆ°åœ¨ç·šçš„文檔: http://dev.voicecloud.cn/developer.php?category=YW5kcm9pZA%3D%3D&column=ZG9jdW1lbnQ%3D&type=d2lkZ2V0 )
通éŽé–±è®€è¨Šé£›çµ¦çš„文檔,å¯ä»¥ç™¼(fÄ)ç¾(xià n)標準的è˜åˆ¥æŽ§ä»¶æ˜¯RecognizerDialog——是一個Dialogçš„å類,所以我們是å¯ä»¥åœ¨Activityä¸é€šéŽshowDialog(int)方法來調(dià o)用它的。
é‡å¯«Activity的方法
- @Override ??
- protected ?Dialog?onCreateDialog( int ?id)?{}??
@Override protected Dialog onCreateDialog(int id) {}
在其ä¸å‰µ(chuà ng)建并è¨(shè)置好一個RecognizerDialogå³å¯ã€‚
?
創(chuà ng)建 RecognizerDialog 方法為
- RecognizerDialog?recognizerDialog?=? new ?RecognizerDialog(??
- ????????MainActivity. this ,? "appid=1234567" ); //?這里應(yīng)該寫從科大訊飛申請到的appid ??
RecognizerDialog recognizerDialog = new RecognizerDialog( MainActivity.this, "appid=1234567");// 這里應(yīng)該寫從科大訊飛申請到的appid
(其ä¸appid應(yÄ«ng)該寫自己從訊飛申請到的appid,由于å”(xié)è°çš„å•é¡Œï¼Œæˆ‘ä¸ä¾¿æŠŠè‡ªå·±ç”³è«‹çš„id公開,所以這里用了1234567ä¾†ä»£æ›¿ã€‚æ ¹æ“š(jù)訊飛的說明,éžæ³•çš„appid是ä¸èƒ½ä½¿ç”¨èªžéŸ³æœå‹™(wù)的,但是我用隨機數(shù)å—試驗了一下,居然也是能用的,ä¸çŸ¥é“是ä¸æ˜¯BUG。)
?
? ? ? ? ? ? ? ? ? ? ? ? ? 官方的文檔:
? ? ? ?
然åŽéœ€è¦è¨(shè)ç½®è˜åˆ¥åƒæ•¸(shù)
- recognizerDialog.setEngine( "sms" ,? null ,? null );??
recognizerDialog.setEngine("sms", null, null);
第一個åƒæ•¸(shù)“smsâ€è¡¨ç¤ºç‚ºè«‹æ±‚çš„æœå‹™(wù)為“語音è˜åˆ¥â€ã€‚åŽå…©å€‹åƒæ•¸(shù)暫時è¨(shè)為null就好。
?
? ? ? ? ? ? ?官方文檔:
最åŽé‚„需給recognizerDialogè¨(shè)置一個listener,回調(dià o)接å£ç”¨ä»¥ç²å–çµ(jié)果,
recognizerDialog.setListener(RecognizerDialogListener listener)çš„åƒæ•¸(shù)為RecognizerDialogListener接å£ï¼Œå¯¦ç¾(xià n)æ¤æŽ¥å£è¦é‡å¯«å…©å€‹æ–¹æ³•ï¼šonResults(ArrayList results,boolean isLast)å’ŒonEnd(SpeechError error)。用以ç²å–和處ç†çµ(jié)果。
我的實ç¾(xià n)為直接寫了一個匿å類:
- recognizerDialog.setListener( new ?RecognizerDialogListener()?{??
- ???? @Override ??
- ???? public ? void ?onResults(ArrayList<RecognizerResult>?results,??
- ???????????? boolean ?arg1)?{??
- ????????StringBuffer?result?=? new ?StringBuffer();??
- ???????? for ?(RecognizerResult?r?:?results)?{??
- ????????????result.append(r.text);??
- ????????}??
- ????????editText.setText(result.toString());??
- ????}??
- ??
- ???? @Override ??
- ???? public ? void ?onEnd(SpeechError?arg0)?{??
- ??
- ????}??
- });??
recognizerDialog.setListener(new RecognizerDialogListener() { @Override public void onResults(ArrayList<RecognizerResult> results, boolean arg1) { StringBuffer result = new StringBuffer(); for (RecognizerResult r : results) { result.append(r.text); } editText.setText(result.toString()); } @Override public void onEnd(SpeechError arg0) { } });
然åŽå°‡æ¤RecognizerDialog返回å³å¯ã€‚
?
完整的MainActivity代碼:
?
- import ?com.iflytek.speech.RecognizerResult;??
- import ?com.iflytek.speech.SpeechError;??
- import ?com.iflytek.ui.RecognizerDialog;??
- import ?com.iflytek.ui.RecognizerDialogListener;??
- ??
- public ? class ?MainActivity? extends ?Activity?{??
- ??
- ????EditText?editText?=? null ;??
- ????Button?startButton?=? null ;??
- ??
- ???? @Override ??
- ???? public ? void ?onCreate(Bundle?savedInstanceState)?{??
- ???????? super .onCreate(savedInstanceState);??
- ????????setContentView(R.layout.main);??
- ????????editText?=?(EditText)?findViewById(R.id.editText);??
- ????????startButton?=?(Button)?findViewById(R.id.button_start);??
- ????????startButton.setOnClickListener( new ?OnClickListener()?{??
- ???????????? @Override ??
- ???????????? public ? void ?onClick(View?v)?{??
- ????????????????showDialog( 1 );??
- ????????????}??
- ????????});??
- ????}??
- ??
- ???? @Override ??
- ???? protected ?Dialog?onCreateDialog( int ?id)?{??
- ????????RecognizerDialog?recognizerDialog?=? new ?RecognizerDialog(??
- ????????????????MainActivity. this ,? "appid=1234567" ); //?這里應(yīng)該寫從科大訊飛申請到的appid ??
- ????????recognizerDialog.setEngine( "sms" ,? null ,? null );??
- ????????recognizerDialog.setListener( new ?RecognizerDialogListener()?{??
- ???????????? @Override ??
- ???????????? public ? void ?onResults(ArrayList<RecognizerResult>?results,??
- ???????????????????? boolean ?arg1)?{??
- ????????????????StringBuffer?result?=? new ?StringBuffer();??
- ???????????????? for ?(RecognizerResult?r?:?results)?{??
- ????????????????????result.append(r.text);??
- ????????????????}??
- ????????????????editText.setText(result.toString());??
- ????????????}??
- ??
- ???????????? @Override ??
- ???????????? public ? void ?onEnd(SpeechError?arg0)?{??
- ??
- ????????????}??
- ????????});??
- ???????? return ?recognizerDialog;??
- ????}??
- }??
import com.iflytek.speech.RecognizerResult; import com.iflytek.speech.SpeechError; import com.iflytek.ui.RecognizerDialog; import com.iflytek.ui.RecognizerDialogListener; public class MainActivity extends Activity { EditText editText = null; Button startButton = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText = (EditText) findViewById(R.id.editText); startButton = (Button) findViewById(R.id.button_start); startButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showDialog(1); } }); } @Override protected Dialog onCreateDialog(int id) { RecognizerDialog recognizerDialog = new RecognizerDialog( MainActivity.this, "appid=1234567");// 這里應(yīng)該寫從科大訊飛申請到的appid recognizerDialog.setEngine("sms", null, null); recognizerDialog.setListener(new RecognizerDialogListener() { @Override public void onResults(ArrayList<RecognizerResult> results, boolean arg1) { StringBuffer result = new StringBuffer(); for (RecognizerResult r : results) { result.append(r.text); } editText.setText(result.toString()); } @Override public void onEnd(SpeechError arg0) { } }); return recognizerDialog; } }
程åºåœ¨çœŸæ©Ÿé‹è¡Œæˆªåœ–,經(jÄ«ng)éŽæª¢é©—,科大訊飛的è˜åˆ¥çŽ‡é‚„是很高的。?
??
?
demo下載地å€ï¼š http://download.csdn.net/detail/barryhappy/4178459
Android基于基于布局嵌套的é é¢å°Ž(dÇŽo)航實ç¾(xià n)
æ›´å¤šæ–‡ç« ã€æŠ€è¡“(shù)交æµã€å•†å‹™(wù)åˆä½œã€è¯(lián)ç³»åšä¸»
微信掃碼或æœç´¢ï¼šz360901061

å¾®ä¿¡æŽƒä¸€æŽƒåŠ æˆ‘ç‚ºå¥½å‹
QQ號è¯(lián)系: 360901061
您的支æŒæ˜¯åšä¸»å¯«ä½œæœ€å¤§çš„動力,如果您喜æ¡æˆ‘çš„æ–‡ç« ï¼Œæ„Ÿè¦ºæˆ‘çš„æ–‡ç« å°æ‚¨æœ‰å¹«åŠ©ï¼Œè«‹ç”¨å¾®ä¿¡æŽƒæ下é¢äºŒç¶ç¢¼æ”¯æŒåšä¸»2å…ƒã€5å…ƒã€10å…ƒã€20å…ƒç‰æ‚¨æƒ³æ的金é¡å§ï¼Œç‹ ç‹ é»žæ“Šä¸‹é¢çµ¦é»žæ”¯æŒå§ï¼Œç«™é•·éžå¸¸æ„Ÿæ¿€æ‚¨ï¼æ‰‹æ©Ÿå¾®ä¿¡é•·æŒ‰ä¸èƒ½æ”¯ä»˜è§£æ±ºè¾¦æ³•ï¼šè«‹å°‡å¾®ä¿¡æ”¯ä»˜äºŒç¶ç¢¼ä¿å˜åˆ°ç›¸å†Šï¼Œåˆ‡æ›åˆ°å¾®ä¿¡ï¼Œç„¶åŽé»žæ“Šå¾®ä¿¡å³ä¸Šè§’掃一掃功能,é¸æ“‡æ”¯ä»˜äºŒç¶ç¢¼å®Œæˆæ”¯ä»˜ã€‚
ã€æœ¬æ–‡å°æ‚¨æœ‰å¹«åŠ©å°±å¥½ã€‘å…ƒ
