Android Development--web Browser Development (1) Version 1.0

Source: Internet
Author: User

1, build a new project Webdemo

2,layout under Activity_main Design Interface (1) to be optimized

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
tools:context= "${relativepackage}.${activityclass}" >

<button
Android:id= "@+id/btn_go"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparentright= "true"
Android:layout_alignparenttop= "true"
android:text= "@string/btn_go"/>

<edittext
Android:id= "@+id/et"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignbaseline= "@+id/btn_go"
Android:layout_alignbottom= "@+id/btn_go"
Android:layout_alignparentleft= "true"
android:layout_toleftof= "@+id/btn_go"
Android:ems= "10"
android:hint= "@string/ed_hint"/>

<webview
Android:id= "@+id/wv"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Android:layout_alignparentleft= "true"
android:layout_below= "@+id/btn_go"/>

Layout interface:

</RelativeLayout>

3, increase the permissions of the connection network

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

4, writing code

Package Com.example.webdemo;

Import Android.annotation.SuppressLint;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.webkit.WebView;
Import android.webkit.WebViewClient;
Import Android.widget.Button;
Import Android.widget.EditText;

//Click event Listener Interface

public class Mainactivity extends Activity implements Onclicklistener {
// statement
Private Button Btn_go;
Private EditText et;
Private WebView WV;

Initialize URL
Private String et_url= "www.baidu.com";


@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Initializing the control
btn_go= (Button) Findviewbyid (R.ID.BTN_GO);
et= (EditText) Findviewbyid (r.id.et);
wv= (WebView) Findviewbyid (R.ID.WV);

Setting a click Listener event for a control
Btn_go.setonclicklistener (this);
Et.setonclicklistener (this);
Wv.setonclicklistener (this);

Get the URL in the input box
Et_url=et.gettext (). toString ();

Calling methods
Webstyle ();
}

Format the webview and its load URL
@SuppressLint ("setjavascriptenabled")
private void Webstyle ()
{
Wv.getsettings (). Setjavascriptenabled (True);
Wv.getsettings (). Setsupportzoom (True);
Wv.setscrollbarstyle (Webview.scrollbars_outside_overlay);
Wv.requestfocus ();
Wv.loadurl ("http://www.baidu.com");
Wv.setwebviewclient (New mywebviewclient ());
}
Click events

@Override
public void OnClick(View v) {
TODO auto-generated Method Stub
String Url_text;
String url_head = "http://";
Url_text=et.gettext (). toString ();
if (!url_text.contains ("http.//")) {
Url_text=url_head.concat (Url_text);}
Wv.loadurl (Url_text);

}

Setwebviewclient class
Class Mywebviewclient extends Webviewclient
{
@Override
public boolean shouldoverrideurlloading (WebView view, String URL) {
TODO auto-generated Method Stub
View.loadurl (Et_url);
return true;

}
}
}

5, running effect,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.