2016-06-13 67 views
-1

我需要實現我的應用程序的搜索,它應該是這樣的如何實現谷歌喜歡玩搜索欄

Google play store search bar

會有人請關心不夠向我解釋它是如何完成的。??在此先感謝

+0

你可以在這裏讀到它:https://開頭開發商.android.com/guide/topics/search/search-dialog.html – REG1

+1

[在操作欄中實現SearchView]的可能重複(http://stackoverflow.com/questions/21585326/implementing-searchview-in-action-bar) – JonasCz

回答

1

您可以爲此創建自定義佈局。 1.採取相對佈局,並給它一個圓形的邊框背景。 2.將漢堡包圖標添加到其中。 3.在右側添加麥克風圖標。 4.在中心添加edittext。 ,然後手動點擊漢堡圖標和麥克風圖標。

我有一些不完全相同的代碼,但您可以相應地編輯和更改它。

<RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginLeft="@dimen/activity_horizontal_margin" 
      android:layout_marginRight="@dimen/activity_horizontal_margin" 
      android:background="@drawable/editext_border"> 

      <ImageView 
       android:id="@+id/iv_search_mic" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_centerVertical="true" 
       android:src="@drawable/ic_mic_green" /> 

      <EditText 
       android:id="@+id/ed_home_searchbar" 
       fontPath="fonts/weblysleekuisl.ttf" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_toLeftOf="@+id/iv_search_icon" 
       android:layout_toRightOf="@+id/iv_search_mic" 
       android:background="@android:color/transparent" 
       android:hint="@string/action_search" 
       android:imeOptions="actionSearch" 
       android:padding="10dp" 
       android:singleLine="true" 
       android:textColor="@color/colorText" 
       android:textCursorDrawable="@drawable/color_cursor" 
       android:textSize="@dimen/text_xxsmall" /> 

      <ImageView 
       android:id="@+id/iv_search_icon" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:layout_centerVertical="true" 
       android:src="@drawable/ic_search_green" /> 

     </RelativeLayout> 

,然後處理像麥克風點擊

searchMic.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       OnSwap.getInstance().trackEvent(TAG, "searchMic", "searchMic Clicked"); 
       Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
       intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
         RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
       intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Search"); 
       startActivityForResult(intent, VOICE_INPUT_REQUEST_CODE); 
      } 
     }); 

打開谷歌語音搜索,然後單擊事件onActivityResult方法

if (requestCode == VOICE_INPUT_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 
       ArrayList<String> matches = data.getStringArrayListExtra(
         RecognizerIntent.EXTRA_RESULTS); 
       int matchSize = matches.size(); 
       for (int index = 0; index < matchSize; index++) { 
        Log.i(TAG, String.valueOf(index) + ": " + matches.get(index)); 
        if (index == 0) { 
         searchbar.setText(matches.get(index)); 
        } 
       } 
      } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) { 
       Status status = PlaceAutocomplete.getStatus(this, data); 
       // TODO: Handle the error. 
       Log.i(TAG, status.getStatusMessage()); 
       Snackbar.make(locationTextView, status.getStatusMessage(), Snackbar.LENGTH_LONG).show(); 

      } else if (resultCode == RESULT_CANCELED) { 
       Snackbar.make(locationTextView, "Canceled", Snackbar.LENGTH_LONG).show(); 
      } 
     } 
+0

Thnks mate ....我沒有去哪裏跟隨這個搜索的東西在線文檔。 – Infamous

+0

樂於幫助哥們:) – Shubham