Directly:
By, the search toolbar has the following functions:
1. Implement speech recognition and obtain keywords
2. When edittext has text input, the file deletion button should be displayed at the end of the component, that is, the X symbol.
3. edittext is seamlessly connected with the search button on the right.
Not all mobile phones support speech recognition. All mobile phones should be judged before starting speech recognition. The Code is as follows:
/*** Fire an intent to start the speech recognition activity. */private void startvoicerecognitionactivity () {intent = new intent (recognizerintent. action_recognize_speech); intent. putextra (recognizerintent. extra_language_model, recognizerintent. export age_model_free_form); // optional text prompt to show to the user when asking them to speakintent. putextra (recognizerintent. extra_prompt, "Speech Recognition Demo"); packagemanager pkgmanager = getpackagemanager (); List <resolveinfo> listresolveinfo = pkgmanager. queryintentactivities (intent, 0); If (listresolveinfo = NULL | listresolveinfo. size () = 0) {// speech recognition is not supported. The alertdialog prompt is displayed in the dialog box. builder = new alertdialog. builder (this); builder. settitle (R. string. speechrecognition); builder. setmessage (R. string. speecherrorhint); builder. setpositivebutton (R. string. OK, null); builder. create (). show ();} else {// display the speech recognition interface startactivityforresult (intent, voice_recognition_request_code );}}
The Code is as follows:
Package lab. sodino. searchbar; import Java. util. arraylist; import Java. util. iterator; import Java. util. list; import android. app. activity; import android. app. activitymanager; import android. app. alertdialog; import android. app. alertdialog. builder; import android. content. context; import android. content. dialoginterface; import android. content. intent; import android. content. PM. packagemanager; import android. content. PM. resolveinfo; import android. graphics. drawable. drawable; import android. graphics. drawable. statelistdrawable; import android. OS. bundle; import android. speech. recognizerintent; import android. text. editable; import android. text. textwatcher; import android. util. log; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext; public class acts Earchbar extends activity implements onclicklistener, android. content. dialoginterface. onclicklistener {Private Static final int voice_recognition_request_code = 1234; private button btnspeech; private button btnsearch; private button btnclearedit; private edittext edtsearch; private string [] arrspeechkeyword; /** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinst Ancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); btnspeech = (button) findviewbyid (R. id. searchbtnspeech); btnspeech. setbackgrounddrawable (newselector (this, R. drawable. search_speech, R. drawable. search_speech_pressed, R. drawable. search_speech); btnspeech. setonclicklistener (this); btnsearch = (button) findviewbyid (R. id. searchbutton); btnsearch. setonclicklistener (this); btnsearc H. setbackgrounddrawable (newselector (this, R. drawable. search, R. drawable. search_pressed, R. drawable. search_pressed); btnclearedit = (button) findviewbyid (R. id. btnclearedit); btnclearedit. setonclicklistener (this); edtsearch = (edittext) findviewbyid (R. id. searchedit); edtsearch. setbackgrounddrawable (newselector (this, R. drawable. search_box, R. drawable. search_box_pressed, R. drawable. search_box_pressed); EDT Search. setonclicklistener (this); edtsearch. addtextchangedlistener (New textwatcher () {public void ontextchanged (charsequence S, int start, int before, int count) {// log. D ("android_lab", "ontextchanged:" + String. valueof (s) + // "Start =" + start + "before =" + before // + "Count =" + count);} public void beforetextchanged (charsequence s, int start, int count, int after) {// log. D ("android_lab", "ontex Tchanged before: "+ // string. valueof (s) + "Start =" + start + "Count =" // + Count + "after =" + after);} public void aftertextchanged (editable S) {// log. D ("android_lab", "ontextchanged after:" + // string. valueof (s); If (S = NULL | S. length () = 0) {log. D ("android_lab", "btnclear gone"); btnclearedit. setvisibility (view. gone);} else {log. D ("android_lab", "btnclear visible"); btnclearedit. setvisibili Ty (view. visible) ;}}) ;}@ overridepublic void onclick (view) {If (view = edtsearch) {edtsearch. setfocusable (true); log. D ("android_lab", "edtsearch");} else if (view = btnsearch) {} else if (view = btnspeech) {startvoicerecognitionactivity ();} else if (view = btnclearedit) {edtsearch. settext ("") ;}}/** sets selector. */Public static statelistdrawable newselector (context, int idnormal, int idpressed, int idfocused) {statelistdrawable BG = new statelistdrawable (); drawable normal = idnormal =-1? Null: context. getresources (). getdrawable (idnormal); drawable pressed = idpressed =-1? Null: context. getresources (). getdrawable (idpressed); drawable focused = idfocused =-1? Null: context. getresources (). getdrawable (idfocused); // view. pressed_enabled_state_setbg.addstate (New int [] {16842910,168 42919}, pressed); // view. enabled_focused_state_setbg.addstate (New int [] {16842908,168 42910}, focused); // view. enabled_state_setbg.addstate (New int [] {16842910}, normal); // view. focused_state_setbg.addstate (New int [] {16842908}, focused); // view. empty_state_setbg.addsta Te (New int [] {}, normal); Return BG;}/*** fire an intent to start the speech recognition activity. */private void startvoicerecognitionactivity () {intent = new intent (recognizerintent. action_recognize_speech); intent. putextra (recognizerintent. extra_language_model, recognizerintent. export age_model_free_form); // optional text prompt to show to the user when asking them to speakintent. putextra (R Ecognizerintent. extra_prompt, "Speech Recognition Demo"); packagemanager pkgmanager = getpackagemanager (); List <resolveinfo> listresolveinfo = pkgmanager. queryintentactivities (intent, 0); If (listresolveinfo = NULL | listresolveinfo. size () = 0) {// speech recognition is not supported. The alertdialog prompt is displayed in the dialog box. builder = new alertdialog. builder (this); builder. settitle (R. string. speechrecognition); builder. setmessage (R. string. spee Cherrorhint); builder. setpositivebutton (R. string. OK, null); builder. create (). show ();} else {// display the speech recognition interface startactivityforresult (intent, voice_recognition_request_code);}/*** handle the results from the recognition activity. * // @ overrideprotected void onactivityresult (INT requestcode, int resultcode, intent) {If (requestcode = voice_recognition_request_code & resultcode = result_ OK) {// fi Ll the list view with the strings the recognizer thought it // cocould have heardarraylist <string> arrresults = intent. getstringarraylistextra (recognizerintent. extra_results); int size = arrresults. size (); arrspeechkeyword = new string [size]; for (INT I = 0; I <size; I ++) {arrspeechkeyword [I] = arrresults. get (I);} arrresults. clear (); arrresults = NULL; alertdialog. builder = new alertdialog. builder (This ). settitle (R. string. searchspeechhint); builder. setitems (arrspeechkeyword, this); builder. create (). show ();} super. onactivityresult (requestcode, resultcode, intent) ;}@ overridepublic void onclick (dialoginterface dialog, int which) {If (arrspeechkeyword! = NULL & arrspeechkeyword. length> which) {edtsearch. settext (arrspeechkeyword [which]) ;}}
/RES/layout/search_bar.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="40dip" android:layout_marginLeft="10dip" android:layout_marginRight="10dip"> <Button android:layout_width="40dip" android:layout_height="wrap_content" android:id="@+id/searchBtnSpeech" android:layout_gravity="center_vertical|left" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:focusableInTouchMode="true" android:layout_marginRight="10dip" ></Button><Button android:layout_width="40dip"android:layout_height="fill_parent"android:id="@+id/searchButton"android:textColor="#ffffffff"android:textSize="18sp"android:gravity="center"android:layout_marginBottom="-1dip"android:layout_marginTop="0dip"android:layout_alignParentRight="true"android:layout_centerVertical="true"></Button> <EditText android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:id="@+id/searchEdit"android:drawablePadding="5dip"android:singleLine="true"android:hint="@string/searchHint"android:textSize="16sp"android:layout_margin="0dip"android:layout_toRightOf="@id/searchBtnSpeech"android:layout_toLeftOf="@id/searchButton"android:layout_centerVertical="true"></EditText><Button android:layout_width="40dip"android:layout_height="fill_parent"android:id="@+id/btnClearEdit"android:background="@drawable/search_clean"android:layout_alignRight="@id/searchEdit"android:layout_centerVertical="true"></Button></RelativeLayout>
This article is owned by sodino, A csdn blog blogger.
Reprinted please indicate the source: http://blog.csdn.net/sodino/article/details/6889297
History posts involved include: [Android] Code Implementation statelistdrawable
Http://blog.csdn.net/sodino/article/details/6797821