Android WebView Simple browser implementation code _android

Source: Internet
Author: User
Tags button type

File Main.java

Copy Code code as follows:

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 the activity is a. */
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

Copy Code code as follows:

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:id= "@+id/turn"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" >

Copy Code code as follows:

<android:layout_height= "Wrap_content"
Android:baselinealigned= "false" >
<android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" >

Configuration file Androidmanifest.xml need to be set inside

Related Article

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.