1

je l'ai mis en place quelques boutons dans mon content_main.xmlRecherche par le texte d'un bouton à l'aide SearchView

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 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" 
     android:id="@+id/content_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 

     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     tools:context="com.ivmenusisla.MainActivity" 
     tools:showIn="@layout/app_bar_main"> 

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:id="@+id/allRestaurants"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" > 

       <Button 
        android:text="Blenders in the Grass" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:drawableLeft="@drawable/blenders" 
        android:alpha="0.90" 
        android:id="@+id/blenders" /> 

       <Button 
        android:text="Breakfast to Breakfast" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:drawableLeft="@drawable/btob" 
        android:alpha="0.90" 
        android:id="@+id/btob" /> 

      </LinearLayout> 
     </ScrollView> 

    </RelativeLayout> 

J'ai aussi ajouté un widget s SearchView sur mon main.xml

<?xml version="1.0" encoding="utf-8"?> 

    <menu xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto"> 
     <item 
      android:id="@+id/action_search" 
      android:icon="@android:drawable/ic_menu_search" 
      android:title="search" 
      app:actionViewClass="android.support.v7.widget.SearchView" 
      app:showAsAction="always" /> 
    </menu> 

Comment puis-je définir un écouteur d'action de sorte que lorsqu'un utilisateur clique sur le widget SearchView et tape quelque chose, les résultats sont affichés en fonction du texte de chaque bouton?

J'ai aussi sur mon MainActivity.java

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 

    MenuItem searchItem = menu.findItem(R.id.action_search); 
    SearchView searchView = (SearchView) searchItem.getActionView(); 

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 

     @Override 
     public boolean onQueryTextChange(String newText) { 
      //Log.e("onQueryTextChange", "called"); 
      return false; 
     } 

     @Override 
     public boolean onQueryTextSubmit(String query) { 


      // Do your task here 

      return false; 
     } 

    }); 

    return true; 
} 

Merci!

Répondre

0

Dans la fonction "onCreate" de l'activité, vous devez construire une liste contenant le texte des boutons.

List<String> textContent = new ArrayList(); 

... onCreate (...) { 
    ... 
    Button button1 = (Button) findViewById(R.id.blenders); 
    Button button2 = (Button) findViewById(R.id.btob); 

    textContent.clear(); 
    textContent.add(button1.getText()); 
    textContent.add(button2.getText()); 

    ... 
} 


    @Override 
    public boolean onQueryTextSubmit(String query) { 
     // Do your task here 

     for(String text : textContent) { 
      if(text.contain(query)) { 
       // do what you wanna do 
      } 
     } 

     return false; 
    } 
+0

Merci! Je ferai comme tu as dit! –