In Android, open the browser to the specified Webpage Through intent

Source: Internet
Author: User

The android system has many built-in applications, including dialing, text messages, and browsers. Here we create a simple Android program and call the built-in browser to open the specified address.

The interface is as follows:

The corresponding layout xml is:

<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">

<Button

Android: id = "@ + id/btnGo"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: layout_alignParentRight = "true"

Android: layout_alignParentTop = "true"

Android: layout_marginTop = "46dp"

Android: text = "@ string/btnTitle_go"/>

<EditText

Android: id = "@ + id/txtUri"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: layout_alignBaseline = "@ + id/btnGo"

Android: layout_alignBottom = "@ + id/btnGo"

Android: layout_alignParentLeft = "true"

Android: layout_toLeftOf = "@ + id/btnGo"

Android: EMS = "10"

Android: text = "http://junqilian.cnblogs.com">

<RequestFocus/>

</EditText>

</RelativeLayout>

The Java code is implemented as follows. It mainly adds an OnKeyListener to EditText, processes the onClickListener in editText, presses the Enter key, adds an onClickListener to the button, triggers the OpenBroswer function, and opens the built-in browser through intent.

Package com. example. openbroswer;

Import android.net. Uri;

Import android. OS. Bundle;

Import android. app. Activity;

Import android. content. Intent;

Import android. view. KeyEvent;

Import android. view. Menu;

Import android. view. View;

Import android. view. View. OnClickListener;

Import android. view. View. OnKeyListener;

Import android. widget. Button;

Import android. widget. EditText;

Public class MainActivity extends Activity {

Private EditText urlText;

Private Button goButton;

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. activity_main );

UrlText = (edittext=findviewbyid(r.id.txt Uri );

GoButton = (Button) findViewById (R. id. btnGo );

// Setup event handler

UrlText. setOnKeyListener (new OnKeyListener (){

Public boolean onKey (View view, int keyCode, KeyEvent event ){

If (keyCode = KeyEvent. KEYCODE_ENTER ){

OpenBrower ();

Return true;

}

Return false;

}

});

GoButton. setOnClickListener (new OnClickListener (){

Public void onClick (View view ){

OpenBrower ();

}

});

}

Protected void openBrower (){

Uri uri = Uri. parse (urlText. getText (). toString ());

Intent intent = new Intent (Intent. ACTION_VIEW, uri );

StartActivity (intent );

}

@ Override

Public boolean onCreateOptionsMenu (Menu menu ){

GetMenuInflater (). inflate (R. menu. activity_main, menu );

Return true;

}

}

The code is not complex, but from. net + visual studio, it is still not used...

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.