2017-03-19 103 views
1
一個按鈕的文本搜索

我在content_main.xml實施了一些按鈕通過搜索查看

<?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> 

我還增補搜索查看插件在我的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> 

我怎麼能設置一個動作監聽器,以便當用戶點擊SearchView小部件並鍵入內容時,結果將基於每個按鈕的文本顯示出來?

我也有這個在我的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; 
} 

謝謝!

回答

0

在Activity的「onCreate」函數中,您應該構造一個包含按鈕文本的列表。

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

謝謝!我會照你說的去做! –