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: