android中使用WebView浏览网页

布局文件如下:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context=".MainActivity" >
 7 
 8     <EditText
 9         android:id="@+id/url"
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content" />
12 
13     <Button
14         android:id="@+id/search"
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content" 
17         android:text="搜索"/>
18 
19     <WebView
20         android:id="@+id/show"
21         android:layout_width="match_parent"
22         android:layout_height="match_parent" />
23 
24 </LinearLayout>

然后是mainactivity中代码:

 1 public class MainActivity extends Activity {
 2     EditText url;
 3     WebView show;
 4     Button search;
 5 
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.activity_main);
10         url = (EditText) findViewById(R.id.url);
11         show = (WebView) findViewById(R.id.show);
12         search = (Button) findViewById(R.id.search);
13         search.setOnClickListener(new OnClickListener() {
14             
15             public void onClick(View v) {
16                 String urlStr = url.getText().toString();
17                 show.loadUrl(urlStr);
18             }
19         });
20     }
21 
22 }

由于要联网,在androidmanifest配置文件中加入网络访问权限:

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

 

posted on 2017-05-25 18:01  baorant  阅读(291)  评论(0编辑  收藏  举报

导航