查找图片

结果效果图


代码如下:

<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中}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值