我正在尝试在 android 中实现搜索功能来搜索按钮。通常,搜索用于搜索listviews
,但我想用它来搜索按钮。我的活动中有 118 个按钮,edittext
顶部有一个(搜索栏)。每个按钮代表一种化学元素的符号。
例如:
Cu = 铜
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"};
请帮我实现搜索到搜索按钮!
感谢您的时间!