结果效果图
代码如下:
<LinearLayout 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:orientation="vertical"
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" >
<ImageView
android:id="@+id/ivImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" />
<EditText
android:id="@+id/etImageUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="请输入图片的地址" />
<Button
android:id="@+id/btnView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/button_bg"
android:onClick="viewImage"
android:text="浏览" />
</LinearLayout>
public class Dictionary extends Activity implements OnClickListener,
TextWatcher {
private DBHelper dbHelper; // 用户输入文本框
private AutoCompleteTextView word; // 定义数据库的名字
private SQLiteDatabase database;
private Button searchWord; // 搜索按钮
private TextView showResult; // 用户显示查询结果
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new DBHelper(getBaseContext());// 打开数据库
database = dbHelper.openDatabase();
init();
searchWord.setOnClickListener(this); // 绑定监听器
word.addTextChangedListener(this); // 绑定文字改变监听器
}
public void init() {
searchWord = (Button) findViewById(R.id.btnSearch);
word = (AutoCompleteTextView) findViewById(R.id.etWord);
showResult = (TextView) findViewById(R.id.tvSearchResult);
}
public void afterTextChanged(Editable s) {
Cursor cursor = database.rawQuery(
"select english as _id from t_words where english like ?",
new String[] { s.toString() + "%" });
// 新建新的Adapter
DictionaryAdapter dictionaryAdapter = new DictionaryAdapter(this,cursor, true);
// 绑定适配器
word.setAdapter(dictionaryAdapter);
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void onClick(View view) {
// 查询指定的单词
String sql = "select chinese from t_words where english=?";
Cursor cursor = database.rawQuery(sql, new String[] { word.getText()
.toString() });
String result = "查无该词"; // 如果查找单词,显示其中文的意思
if (cursor.getCount() > 0) {
cursor.moveToFirst(); // 须使用moveToFirst方法将记录指针移动到第1条记录的位置
result = cursor.getString(cursor.getColumnIndex("chinese"))
.replace("&", "&");
}
showResult.setText(word.getText() + "\n" + result.toString());// 将结果显示到TextView中
}
输入网址即可得到图片
public class Dictionary extends Activity implements OnClickListener,TextWatcher {private DBHelper dbHelper; // 用户输入文本框private AutoCompleteTextView word; // 定义数据库的名字private SQLiteDatabase database;private Button searchWord; // 搜索按钮private TextView showResult; // 用户显示查询结果@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);dbHelper = new DBHelper(getBaseContext());// 打开数据库database = dbHelper.openDatabase();init();searchWord.setOnClickListener(this); // 绑定监听器word.addTextChangedListener(this); // 绑定文字改变监听器}public void init() {searchWord = (Button) findViewById(R.id.btnSearch);word = (AutoCompleteTextView) findViewById(R.id.etWord);showResult = (TextView) findViewById(R.id.tvSearchResult);}public void afterTextChanged(Editable s) {Cursor cursor = database.rawQuery("select english as _id from t_words where english like ?",new String[] { s.toString() + "%" });// 新建新的AdapterDictionaryAdapter dictionaryAdapter = new DictionaryAdapter(this,cursor, true);// 绑定适配器word.setAdapter(dictionaryAdapter);}public void beforeTextChanged(CharSequence s, int start, int count,int after) {}public void onTextChanged(CharSequence s, int start, int before, int count) {}public void onClick(View view) {// 查询指定的单词String sql = "select chinese from t_words where english=?";Cursor cursor = database.rawQuery(sql, new String[] { word.getText().toString() });String result = "查无该词"; // 如果查找单词,显示其中文的意思if (cursor.getCount() > 0) { cursor.moveToFirst(); // 须使用moveToFirst方法将记录指针移动到第1条记录的位置result = cursor.getString(cursor.getColumnIndex("chinese")).replace("&", "&");}showResult.setText(word.getText() + "\n" + result.toString());// 将结果显示到TextView中}