搜索高亮按钮 [英] Highlight buttons on search

查看:254
本文介绍了搜索高亮按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现在android系统的搜索功能来搜索按钮。通常情况下,搜索用于搜索列表视图但我想用它来搜索按钮。我在我的活动118的按钮和有顶部的的EditText (搜索栏)。每个按钮重新presents一种化学元素的符号。结果
例如:结果
铜=铜结果
锌=锌结果
O =氧结果
F =氟结果
CL =氯结果
和112多....

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

这是我的行为看起来像结果
(此屏幕截图只是为了更好地解释我的问题,这是不实际的活动)的

结果
这张图片只是显示6个按​​钮,但我有更多的112。搜索栏看起来是一样的。结果
结果现在,这就是我想要的目的。搜索结果

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.

我要立刻凸显其重新presents该类型的元素的按钮。用户将键入该元素的名称,但它重新$ P $按钮psents其符号应突出。我怎样才能做到这一点?

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?

我读 TextWatchers(),并用它们来搜索列表视图的方式:

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                          
}
});

下面是上面的截图的XML。

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!

推荐答案

创建一个函数doSearchAction以下提到:

create a function doSearchAction as mentioned below :

//执行搜索行动需要上onSearch事件即搜索栏的文本观察家被调用。

// 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)


        }

这篇关于搜索高亮按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆