2016-01-16 18 views
12

Dobrzy chłopaki dni.Dodaj ikonę wyszukiwania na pasku działań Android

mam ikonę wyszukiwania w pasku akcji jako obrazek poniżej

enter image description here

Po jego kliknięciu, chcę zmiany bar działanie do editText i ma przeszukiwanie icon obok editText

I wiedzieć, jak zrobić editText z obrazem, ale jak umieścić editText na pasku akcji jak na zdjęciu poniżej? I chcę ustawić wartość wyświetlania danych, gdy parametr editText zostanie wypełniony wartością. Czy powinienem użyć intencji?

enter image description here

To co próbowałem do tej pory.

aktywny Strzał

getData(deviceName, month); // retrieve data from `MySQL` and load into listView 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      MenuInflater inflater = getMenuInflater(); 
      inflater.inflate(R.menu.create_menu, menu); 
      return true; 
     } 

     @Override 
     public boolean onPrepareOptionsMenu(Menu menu) { 
      mSearchAction = menu.findItem(R.id.search); 
      return super.onPrepareOptionsMenu(menu); 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      // Handle action bar item clicks here. The action bar will 
      // automatically handle clicks on the Home/Up button, so long 
      // as you specify a parent activity in AndroidManifest.xml. 
      switch (item.getItemId()) { 

       case R.id.search: // should I need to add intent ? 
        handleMenuSearch(); 
        return true; 

       case R.id.add: // create new file 
        View menuItemView = findViewById(R.id.add); 
        PopupMenu po = new PopupMenu(HomePage.this, menuItemView); //for drop-down menu 
        po.getMenuInflater().inflate(R.menu.popup_menu, po.getMenu()); 
        po.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 
         public boolean onMenuItemClick(MenuItem item) { 
          // Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show(); 
          if ("Create New File".equals(item.getTitle())) { 
           Intent intent = new Intent(HomePage.this, Information.class); // go to Information class 
           startActivity(intent); 

          } else if ("Edit File".equals(item.getTitle())) { 
           Intent intent = new Intent(HomePage.this, Edit.class); 
           startActivity(intent); 
          } 
          return true; 
         } 
        }); 
        po.show(); //showing popup menu 

      } 
      return super.onOptionsItemSelected(item); 
     } 

     protected void handleMenuSearch(){ 
      ActionBar action = getSupportActionBar(); //get the actionbar 

      if(isSearchOpened){ //test if the search is open 

       action.setDisplayShowCustomEnabled(false); //disable a custom view inside the actionbar 
       action.setDisplayShowTitleEnabled(true); //show the title in the action bar 

       //hides the keyboard 
       InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.hideSoftInputFromWindow(edtSeach.getWindowToken(), 0); 

       //add the search icon in the action bar 
       mSearchAction.setIcon(getResources().getDrawable(R.mipmap.search)); 

       isSearchOpened = false; 
      } else { //open the search entry 

       action.setDisplayShowCustomEnabled(true); //enable it to display a 
       // custom view in the action bar. 
       action.setCustomView(R.layout.search_bar);//add the custom view 
       action.setDisplayShowTitleEnabled(false); //hide the title 

       edtSeach = (EditText)action.getCustomView().findViewById(R.id.edtSearch); //the text editor 

       //this is a listener to do a search when the user clicks on search button 
       edtSeach.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
        @Override 
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
         if (actionId == EditorInfo.IME_ACTION_SEARCH) { 
          doSearch(); 
          return true; 
         } 
         return false; 
        } 
       }); 


       edtSeach.requestFocus(); 

       //open the keyboard focused in the edtSearch 
       InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.showSoftInput(edtSeach, InputMethodManager.SHOW_IMPLICIT); 


       //add the close icon 
       mSearchAction.setIcon(getResources().getDrawable(R.mipmap.search)); 

       isSearchOpened = true; 
      } 
     } 

     @Override 
     public void onBackPressed() { 
      if(isSearchOpened) { 
       handleMenuSearch(); 
       return; 
      } 
      super.onBackPressed(); 
     } 

     private void doSearch() { 
    // 
     } 


    } 

Ekran działalności dla

enter image description here

Podczas wyszukiwania ikona jest wciśnięty, to ma zamiaru innej strony i mieć ikonę wyszukiwania na the editText (dokładnie jak drugi obraz), ale tak się nie stało. Pozostaje na tej samej stronie.

enter image description here

To jest to, co chcę (Z WeChat)

Przed ikona wciśnięty

enter image description here

Po naciśnięciu

enter image description here

Odpowiedz

22

To widżet nazwa brzmi: android.support.v7.widget.SearchView

http://developer.android.com/reference/android/widget/SearchView.html

W menu:

<item 
     android:id="@+id/action_search" 
     android:icon="@drawable/abc_ic_search_api_mtrl_alpha" 
     android:title="@string/srch" 
     app:actionViewClass="android.support.v7.widget.SearchView" 
     app:showAsAction="ifRoom|collapseActionView" /> 

Java:

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     // Retrieve the SearchView and plug it into SearchManager 
     final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search)); 
     SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE); 
     searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 

     return true; 
    } 

To jest dokładnie tak, jak MaterialDesign ikony.

android:icon="@drawable/abc_ic_search_api_mtrl_alpha" 
+0

Czy powinienem użyć intencji, gdy kliknięto ikonę wyszukiwania, aby powiązane dane pojawiły się, gdy editText wypełni się wartością? – Hoo

+0

To zależy od oczekiwań, z moją odpowiedzią możesz to zrobić bez użycia intencji. – Mohsen

+4

android: icon = "@ android: drawable/ic_menu_search" – Jaydev

6

Powinieneś używać wbudowanego androida SearchView to trzymaj się zgodnie z Androidem UI Wzory

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".ui.home.activities.TransactionSearchActivity"> 
    <item 
     android:id="@+id/action_search" 
     android:icon="@android:drawable/ic_menu_search" 
     android:title="@string/search" 
     app:actionViewClass="android.support.v7.widget.SearchView" 
     app:showAsAction="ifRoom" /> 
</menu> 

kod aktywny

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_transaction_search, menu); 
     final MenuItem searchItem = menu.findItem(R.id.action_search); 

     if (searchItem != null) { 
      searchView = (SearchView) MenuItemCompat.getActionView(searchItem); 
      searchView.setOnCloseListener(new SearchView.OnCloseListener() { 
       @Override 
       public boolean onClose() { 
       //some operation 
       } 
      }); 
      searchView.setOnSearchClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        //some operation 
       } 
      }); 
      EditText searchPlate = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text); 
      searchPlate.setHint("Search"); 
      View searchPlateView = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate); 
      searchPlateView.setBackgroundColor(ContextCompat.getColor(this, android.R.color.transparent)); 
      // use this method for search process 
      searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 
       @Override 
       public boolean onQueryTextSubmit(String query) { 
        // use this method when query submitted 
        Toast.makeText(context, query, Toast.LENGTH_SHORT).show(); 
        return false; 
       } 

       @Override 
       public boolean onQueryTextChange(String newText) { 
        // use this method for auto complete search process 
        return false; 
       } 
      }); 
      SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE); 
      searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 

     } 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public void onBackPressed() { 
     if (!searchView.isIconified()) { 
      searchView.setIconified(true); 
      findViewById(R.id.default_title).setVisibility(View.VISIBLE); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

upewnij się menu nadmuchać: D

+0

Czy możesz ponownie sprawdzić mój wpis? powinienem użyć intencji? – Hoo

+0

aktualizujesz swoje dane w tym samym działaniu, w którym używasz tego menu opcji. Do tego nie jest wymagane żadne działanie. – sector11

+0

, więc po kliknięciu przycisku Wstecz (obok editText) pojawi się styczniowy listView? – Hoo

Powiązane problemy