2017-03-19 104 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

谢谢!我会照你说的去做! –