练习:WebView控件使用(链接有道词典查单词)

一。练习结果

1.在editorText输入英语单词,点击查询,下方webView控件显示有道词典中的翻译。

2.结果图片:


二:分析

与其他的控件一样,WebView(网络视图)能加载显示网页,可以将其视为一个浏览器。它使用了WebKit渲染引擎加载显示网页。

三:代码

MainActivity:

package com.example.yd_webview;

import android.os.Bundle;
import android.app.Activity;
import android.text.style.ClickableSpan;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	//声明控件
	private EditText edt_select;
	private Button btn_select;
	private Button btn_clern;
	private WebView webView_select;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		getViews();
		//清空
		btn_clern.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				edt_select.setText("");
			}
		});
		//查询
		btn_select.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String strURI=edt_select.getText().toString();
				strURI=strURI.trim();
				//如果内容为空
				if(strURI.length()==0){
					Toast.makeText(MainActivity.this, "查询内容不能为空", Toast.LENGTH_LONG).show();
				}else{
					String strURL="http://dict.youdao.com/search?q="+strURI;
					webView_select.loadUrl(strURL);
				}
			}
		});
		
	}
	/*获取控件*/
	public void getViews(){
		edt_select=(EditText) findViewById(R.id.edt_select);
		btn_select=(Button) findViewById(R.id.btn_select);
		btn_clern=(Button) findViewById(R.id.btn_clern);
		webView_select=(WebView) findViewById(R.id.webView_select);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
}
Activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/edt_select"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:maxLines="1" >
        </EditText>

        <Button
            android:id="@+id/btn_select"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="查询" />

        <Button
            android:id="@+id/btn_clern"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="清空" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <WebView
            android:id="@+id/webView_select"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

</LinearLayout>
AndroidMainfest.xml中添加联网权限:

    <uses-permission android:name="android.permission.INTERNET"/>

四:相关下载

http://download.csdn.net/download/yyd_diablo/8833173

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值