SoFunction
Updated on 2025-04-10

Android implementation webview instance code

Webview is a very simple function. The code is not logically difficult, but you need to pay attention to permissions. In fact, during the Android programming process, permission problems can be regarded as a relatively large number of bugs.

package .webview01;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class MainActivity extends Activity {

  private EditText et_url;
  private Button btn_request;
  private WebView wv;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);

    initView();
    initSetting();
    initEvent();

  }

  private void initSetting() {
    //webview setting properties    WebSettings ws=();
    (true);
    ();
    (true);
    (true);
    //Click the link to continue to respond in the current browser, rather than responding to the link in the newly opened Android system browser. You must override the webview WebViewClient object    (new WebViewClient(){    
      public boolean shouldOverrideUrlLoading(WebView view, String url) {    
        (url);    
        return true;    
      }    
    });  

  }

  //Initialize the control  private void initView() {
    et_url =(EditText)findViewById(.et_url);    
    btn_request = (Button)findViewById(.btn_request);    
    wv= (WebView)findViewById();   
  }
  //Key monitoring  public boolean onKeyDown(int keyCode, KeyEvent event) {    
    if ((keyCode == KeyEvent.KEYCODE_BACK) && ()) {    
      ();    
      return true;    
    }    
    return (keyCode, event);    
  } 
  //Initialize event  private void initEvent() {

    btn_request.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        String url=et_url.getText().toString().trim();
        (url);
      }
    });
  }

}

2. The most important thing is to add permissions in the manifest, otherwise it will not be displayed.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:andro
  package=".webview01"
  android:versionCode="1"
  android:versionName="1.0" >

  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
  <uses-permission android:name=""/>

  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
      android:name="."
      android:label="@string/app_name" >
      <intent-filter>
        <action android:name="" />

        <category android:name="" />
      </intent-filter>
    </activity>
  </
app````
ication>

</manifest>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.