启动后效果图:
activity-main.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"
android:background="@drawable/mainbg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<AutoCompleteTextView
android:id="@+id/etWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="31dp"
android:background="@android:drawable/edit_text"
android:ems="10"
android:hint="@string/searchHint"
android:singleLine="true"
android:textColor="#552006"
android:textColorHint="#782f10" >
</AutoCompleteTextView>
<Button
android:id="@+id/btnSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/etWord"
android:layout_alignBottom="@+id/etWord"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/etWord"
android:background="@drawable/ibsearchword"
android:onClick="searchWord"
android:text="@string/serachWord" />
<TextView
android:id="@+id/tvSearchResult"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="@+id/etWord"
android:layout_below="@+id/etWord"
android:layout_marginTop="22dp"
android:textSize="25sp"
android:background="@drawable/bg_roundcorner"
android:textAppearance="?android:attr/textAppearanceMedium" />
效果图:
?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">电子词典</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="searchHint">请输入需要查询的单词</string>
<string name="serachWord">查询</string>
</resources>
dictionary.adapter.java:
//自定义Adapter类
public class DictionaryAdapter extends CursorAdapter {
private LayoutInflater layoutInflater;
@Override
public CharSequence convertToString(Cursor cursor) {
return cursor == null ? "" : cursor.getString(cursor
.getColumnIndex("_id"));
}
// 将单词信息显示到列表中
private void setView(View view, Cursor cursor) {
TextView tvWordItem = (TextView) view;
tvWordItem.setText(cursor.getString(cursor.getColumnIndex("_id")));
}
// 绑定选项到列表中
@Override
public void bindView(View view, Context context, Cursor cursor) {
setView(view, cursor);
}
// 生成新的选项
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = layoutInflater.inflate(R.layout.word_list_item, null);
setView(view, cursor);
return view;
}
public DictionaryAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
}