WebView学习
来源:互联网 发布:ta热分析软件 编辑:程序博客网 时间:2024/06/06 03:25
MiniBrowser.Java
- public class MiniBrowser extends Activity {
-
- EditText url;
- WebView showWebView;
- Button searchButton;
- String urlStr = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- url = (EditText) findViewById(R.id.url);
- showWebView = (WebView) findViewById(R.id.show);
- searchButton = (Button) findViewById(R.id.searchButton);
-
-
- showWebView.setWebViewClient(new WebViewClient(){
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
-
-
- view.loadUrl(url);
- return true;
- }
- });
-
-
- showWebView.getSettings().setJavaScriptEnabled(true);
-
- searchButton.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
- urlStr = url.getText().toString();
- showWebView.loadUrl(urlStr);
-
- }
- });
-
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
-
-
- if(keyCode == KeyEvent.KEYCODE_BACK){
- showWebView.goBack();
- return true;
- }
- return super.onKeyDown(keyCode, event);
- }
-
- }
mail.xml 布局文件- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- tools:context=".MiniBrowser" >
-
- <EditText
- android:id="@+id/url"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:singleLine="true"
- android:layout_toLeftOf="@+id/searchButton"
- android:layout_alignTop="@+id/searchButton"
- android:layout_alignBottom="@+id/searchButton" />
-
- <Button
- android:id="@+id/searchButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:text="@string/search" />
-
-
- <WebView
- android:id="@+id/show"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_below="@id/searchButton"/>
- </RelativeLayout>
Manife文件设置允许上网- <uses-permission android:name="android.permission.INTERNET"/>
在真机的测试结果
0 0