我正在尝试在android中实现搜索功能来搜索按钮。通常,搜索用于搜索listviews
,但我想用它来搜索按钮。我的活动中有118个按钮,顶部有一个edittext
(搜索栏)。每个按钮代表化学元素的符号
例如:
铜=铜
Zn =锌
O =氧气
F =氟
Cl =氯
还有112个....
这就是我的活动的样子
(此屏幕截图只是为了更好地解释我的问题,而不是实际活动)
这张图片只显示了6个按钮,但我还有112个按钮。搜索栏看起来一样
现在,这就是我想要实现的目标。
我想立即突出显示代表键入元素的按钮。用户将键入元素的名称,但应突出显示代表其符号的按钮。我怎样才能做到这一点?
我了解了TextWatchers()
以及使用它们搜索列表视图的方式:
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。
<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>
这是包含所有化学元素名称的数组:
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"};
请帮我实现搜索按钮搜索!
谢谢你的时间!
答案 0 :(得分:0)
创建一个函数doSearchAction,如下所述:
//执行搜索操作需要在onSearch事件上调用,即搜索栏文本观察器。
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);
}
//将所有按钮重置为默认的drawables
private void resetAllButton()
{
// reset all button to default value
btn1.setBackgroundResource(R.drwable.normal_img)
....
all_buttons.setBackgroundResource(R.drwable.normal_img)
}