Learn how to write 100 Web pages 100 days, I also use 100 days to write 100 complete Android program.
The most basic requirements for these programs:
1 Integrity-Each program must be independent and operational
2 Non-repeatability-the key points of knowledge used are different
Opening chapter:
--Reference Book of this chapter: Hello Android
Write a simple page, as shown below, in Figure 1:
Enter the URL in the text box, Figure 2:
Click on the button go, then you can open this website, Figure 3:
Steps:
First, create a new project, the specific parameter settings can be referred to my project settings, as follows:
Mainly look
Browser.java under 1 src: main java logic code
Layout of the Activity_browser.xml,xml written under Layout 2
3 values of Strings.xml will need to define some characters
4 Androidmanifest.xml is the definition file for the software.
Two set layout layouts, open activity_broswser.xml
Add code:
<linearlayout 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 " android:orientation= "Horizontal" tools:context= "Su.bi.browerserintent.Browser" > <edittext android:id= "@+id/url_field" android:layout_width= "0dip" android:layout_height= "Wrap_content " Android:layout_weight= "1.0" android:imeoptions= "Actiongo" android:inputtype= "Texturi" android: lines= "1"/> <button android:id= "@+id/go_button" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "@string/go_button"/></linearlayout>
Knowledge Points:
1 Use linearlayout layout, add a edittext is the picture of the first has a lower horizontal box, accept the input of the URL note where the layout_width settings, zero, then the layout_weight= "1.0" The box expands all the remaining space in the row, so the layout_width is set directly to zero.
2 of the parameters imeoptions= "Actiongo" and Imputtype= "Texturi" is to tell Android to use the soft keyboard should be with ". com" and "/" and have go button, press this button can jump directly to the specified page.
So we're going to make a 1 interface.
Two Logic code Browser.java
Next is the logic code that drives the interface's work.
1 Set up process input and key functions.
protected void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_browser); Get a handle to all user interface elements urltext = (EditText) Findviewbyid (R.id.url_field); Gobutton = (Button) Findviewbyid (R.id.go_button); Set Up Handlers Gobutton.setonclicklistener (new Onclicklistener () {public void OnClick (View v) { Openbrowser (); } }); Urltext.setonkeylistener (New Onkeylistener () {public Boolean OnKey (View v, int keycode, keyevent event) { if ( Equ (KeyCode, Keyevent.keycode_enter)) { openbrowser (); return true; } return false;}} );
The Openbrowser function is customized below.
Gobutton This button responds to the Click event, so use Setonclicklistener (new Onclicklistener ()) to set the Onclicklistener object Listener This button, this is the Java unique mechanism, directly in the new Write a onclicklistener inherited class, overriding the OnClick function. C + + cannot write like this.
Urltext is the ID of EditText, use Setonkeylistener to set the object listening, if you press ENTER to respond to the Openbrowser function, of course, now the touch screen phone is generally not a button, but there is a soft keyboard.
Openbroser function Definition:
private void Openbrowser () {<span style= "white-space:pre" ></span>uri Uri = Uri.parse (Urltext.gettext (). ToString ()); <span style= "White-space:pre" ></span>intent Intent = new Intent (Intent.action_view, URI); <span style= "White-space:pre" ></span>startactivity (intent);}
Create a new intent and use your browser to open the URL.
So far, the entire program is ready to run.
But here's the problem:
If the input blog.csdn.net/kenden23, then the program crashes. What is this for?
Re-enter: HTTP://BLOG.CSDN.NET/KENDEN23 will work properly.
Oh, it must be the text that begins with http://To work properly.
This requires a little bit of processing, this is the time when the algorithm comes in handy, the string processing problem--a little sledgehammer the feeling of chicken slaughter.
Redefine the Openbrwoser function:
private static final String Httphead = "http:/";p ublic static<t> boolean equ (t A, T B) {return a = = b;} private void Openbrowser () { String address = Urltext.gettext (). toString (); Address = checkhttpaddress (address); Uri uri = uri.parse (address); Intent Intent = new Intent (Intent.action_view, URI); StartActivity (intent); } private string Checkhttpaddress (string address) { int len = Httphead.length (); Len = Len < Address.length ()? Len:address.length (); String str = address.substring (0, Len); if (equ (str, httphead)) { return address; } return httphead + address; }
Tip: The benefit of customizing equ is not to write = = wrong. Harm? Write a bit more code. Ha ha.
Mainly checkhttpaddress this function, is to determine whether the current user input with "http://", if not, then automatically add, if brought, no tube.
Pay attention to the skill of the algorithm, achieve no bug. No matter what the user enters a string, the program will not crash, but may not find the site, such as:
It's normal to work like this. So it's done!
Android Hundred Day Program Open: Intent Open Web page