我使用的是过滤,而不是自动完成。流程是,您必须填充您拥有的 Arraylist 并将其显示为普通列表。 Filtering thingy 将在您的 EditText 中实现。这是我的一些代码:
第一个:声明您的 EditText 和一个将在其中绘制结果的 RelativeLayout:
etClinicName = (EditText)findViewById(R.id.etClinicName);
btnSearch = (Button) findViewById(R.id.btnSearch);
rlayoutResult = (RelativeLayout) findViewById(R.id.relativelayout_result);
2nd:在你的 OnCreate 中,输入以下代码:
etClinicName.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3)
{
// When user changed the Text
SearchByNameFragmentActivity.this.arrayAdapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after)
{
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
这是我的 XML 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_theme"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="41dp"
android:layout_margin="20dp"
android:orientation="horizontal"
android:background="@drawable/shadow" >
<EditText
android:id="@+id/etClinicName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:hint="@string/hintSearchbranch"
android:inputType="textPersonName"
android:textSize="13sp"
android:background="@drawable/shadow"
android:paddingLeft="1dp"
android:gravity="center_vertical" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="10dp">
<ListView
android:id="@+id/lvlClinicList"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/newline" />
<RelativeLayout
android:id="@+id/relativelayout_result"
android:layout_width="wrap_content"
android:layout_height="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/btnSearch"
android:layout_width="42dp"
android:layout_height="wrap_content"
android:background="@drawable/ic_action_search_black"
android:padding="15dp" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
如果您仍然感到困惑,请随时询问,希望对您有所帮助:D