Highlight buttons on search

混江龙づ霸主 提交于 2019-12-25 04:26:44

问题


I am trying to implement search functionality in android to search for buttons. Usually, search is used to search listviews but I want to use it to search buttons. I have 118 buttons in my activity and there is an edittext(Search bar) on the top. Each button represents the symbol of a chemical element.
eg :
Cu = Copper
Zn = Zinc
O = Oxygen
F = Fluorine
Cl = Chlorine
and 112 more....

This is what my activity looks like
(This screen shot is just to better explain my problem and it is not the actual activity)


This image just shows 6 buttons but I have 112 more. The search bar looks the same.

Now, this is what I am trying to achieve.

I want to instantly highlight the button which represents the typed element. The user will type the name of the element but the button which represents its symbol should be highlighted. How can I achieve this?

I read about TextWatchers() and the way to use them to search listviews :

searchbar.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
    // When user changed the Text
    MainActivity.this.listviewadapter.getFilter().filter(cs);   
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
        int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable arg0) {
    // TODO Auto-generated method stub                          
}
});

Here is the xml for the above screenshot.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Table_main"
    android:background="#000000">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:id="@+id/editText"
    android:layout_alignParentTop="true"
    android:background="@drawable/search_bar"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:padding="10dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:hint="Search" />

<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:text="Zn"
    android:textSize="35dp"
    android:id="@+id/button"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:text="Cu"
    android:textSize="35dp"
    android:id="@+id/button2"
    android:layout_alignTop="@+id/button"
    android:layout_toLeftOf="@+id/button" />

<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:text="O"
    android:textSize="35dp"
    android:id="@+id/button3"
    android:layout_alignTop="@+id/button"
    android:layout_toRightOf="@+id/button" />

<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:text="Cl"
    android:textSize="35dp"
    android:id="@+id/button4"
    android:layout_below="@+id/button"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:text="F"
    android:textSize="35dp"
    android:id="@+id/button5"
    android:layout_alignTop="@+id/button4"
    android:layout_toLeftOf="@+id/button4" />

<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:text="N"
    android:textSize="35dp"
    android:id="@+id/button6"
    android:layout_below="@+id/button3"
    android:layout_toRightOf="@+id/button" />

</RelativeLayout>

Here is the array that contains all the chemical element names:

final String[] elename ={"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Fluorine","Neon","Sodium","Magnesium","Aluminium","Silicon","Phosphorous","Sulphur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium","Chromium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc","Gallium","Germanium","Arsenic","Selenium","Bromine","Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium","Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium","Iodine","Xenon","Caesium","Barium","Lanthanum","Cerium","Praseodymium","Neodymium","Promethium","Samarium","Europium","gadoliium","Terbium","Dysprosium","Holmium","Erbium","Thulium","Ytterbium","Lutetium","Hafnium","Tantalum","Tungsten","Rhenium","Osmium","Iridium","Platinum","Gold","Mercury","Thallium","Lead","Bismuth","Polonium","Astatine","Radon","Francium","Radium","Actinium","Thorium","Protactinium","Uranium","Neptunium","Plutonium","Americium","Curium","Berkelium","Californium","Einsteinium","Fermium","Mendelevium","Nobelium","Lawrencium","Rutherfordium","Dubnium","Seaborgium","Bohrium","Hassium","Meitnerium","Darmstadtium","Roentgenium","Copernicium","Ununtrium","Ununquadium","Ununpentium","Ununhexium","Ununseptium","Ununoctium"};

Please help me implementing search to search buttons!
Thank you for your time!


回答1:


create a function doSearchAction as mentioned below :

// perform search action need to be called on onSearch event i.e searchbar text watcher.

private void doSearchAction(List<Button> btnList)
        {
          resetAllButton();
        for(int i=0;i<btnList.size;i++)
        {
        Button btn = btnList.get(i);
        btn.setBackgroundResource(R.drawable.highlight_img); 
        }

// reset all button to default drawables

   private void resetAllButton()
        {
    // reset all button to default value    
    btn1.setBackgroundResource(R.drwable.normal_img)
    ....

    all_buttons.setBackgroundResource(R.drwable.normal_img)


        }


来源:https://stackoverflow.com/questions/22320013/highlight-buttons-on-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!