Home >Java >javaTutorial >android webview simple browser implementation code

android webview simple browser implementation code

高洛峰
高洛峰Original
2017-01-17 14:44:271894browse

File main.java

package com.HHBrowser.android;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
public class main extends Activity {
    /** Called when the activity is first created. */
 WebView wv;
    Handler handler;
    Button btnButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        wv = (WebView)findViewById(R.id.webView1);
  wv.getSettings().setJavaScriptEnabled(true);
  wv.setScrollBarStyle(0);
  wv.loadUrl("http://www.baidu.com");
  btnButton = (Button)findViewById(R.id.turn);
  btnButton.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    EditText editText = (EditText)findViewById(R.id.editText1);
    String strurl = String.valueOf(editText.getText());
    if(strurl.contains("http://"))
    {
     Log.v("ttt", strurl);
     loadurl(wv,strurl);
    }else {
     Log.v("ttt", strurl);
     loadurl(wv,"http://www.baidu.com");
    }   
   }
  });
  wv.setWebViewClient(new WebViewClient()
  {
   public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
     EditText newText = (EditText)findViewById(R.id.editText1);
     newText.setText(url);
              loadurl(view,url);
              return true;  
   }
  });
    }
    public void loadurl(final WebView view,final String url){
  new Thread(){
   public void run(){
     view.loadUrl(url);
   }
  }.start();
 }
}

2 Layout file main.xml

<http://schemas.android.com/apk/res/Android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
<android:layout_height="wrap_content"
android:baselineAligned="false">
     <android:text=""
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText1"
     android:maxEms="15"
     android:minEms="10"
     android:layout_gravity="center_vertical"
     >

android webview 简单浏览器实现代码

   <android:layout_height="wrap_content"
        android:baselineAligned="false">
     <android:layout_width="fill_parent" 
    android:layout_height="wrap_content">

Configuration file AndroidManifest.xml needs to be set

android webview 简单浏览器实现代码

For more articles related to android webview simple browser implementation code, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn