The cool looks are already there, so let's start with the real thing. Forcible, Huaquanxiutui can not do oh, we need a real skill, need a self-improvement heart oh, often think of their own dreams, so that they will not lose their own, will be in the boundless world to find their way forward! I'm not going to tell you that I've just seen "voice of" session 3.
From the last two tutorials, we have set out the following beautiful layout, welcome to page lookup:
Android self-study Development of the fourth chapter of the layout
Android self-study development of the fifth chapter of the layout
Add a second layout
about layout Layout We no longer repeat, here add an extra layout page to display search results, we still choose LinearLayout, this layout is also viewgroup instantiation, We add a view inside to show the result, and this view is WebView.
<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" >
<webview android:id= "@+id/wv_result"
android:layout_width= "Match_parent"
android:layout_height= "match_parent"/>
</LinearLayout>
and define WebView ID as Wv_result,ok, this layout we call it show_result.xml, this page is so simple.
Add a myactivity code
the next show is coming, first look at the file structure, otherwise the paper on the soldier, I can not stand.
This myactivity is a change from the default generated activity from the creation of this helloandroid project, this does not matter what name, we open it first, remember we have this activity bound to search this layout? Let's start with a brief description of each activity with multiple callback functions (that is, the ONXXX series function on the beginning), including Oncreate,onstart,onresume,onstop, These functions describe the life cycle of this activity (life-cycle), which is important. I'll give myself a review in detail, what we need to do is add our own code in OnCreate, which is run by MyActivity as soon as it is opened.
I'm going to add three lines of code to this oncreate function in myactivity.
Setcontentview We first temporarily understood as the binding function, we bound the search this layout.
Setup is a function that I created myself, primarily to bind and initialize the individual view of this layout and to bind the listener events to them.
GetResult also created for me, mainly to pass click event Information and get search results.
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.search);
Setup ();
GetResult ();
}
The Setup function is as follows
public void Setup () {
msearchbtn = (Button) Findviewbyid (r.id.btn_search);
Menteredittext = (edittext) Findviewbyid (r.id.edit_message);
}
This setup is simple and gets search buttons and search boxes.
The GetResult function is as follows
public void GetResult () {
Msearchbtn.setonclicklistener (new View.onclicklistener () {
@Override
public void OnClick (view view) {//to change body '
implemented methods use File | Settings | File Templates.
String entercontent = Menteredittext.gettext (). toString ();
Intent in = new Intent (myactivity.this, resultactivity.class);
In.putextra (Extra_message, entercontent);
StartActivity (in);}}
);
GetResult function for the search button is bound to click on the Listener (x), once the user clicks on the search button, we move to get the contents of the search box and encapsulated into the extra_message, and then the Extra_ The message is attached to the intent and the search results page is called using the StartActivity function.
Code to add search results
the action of the search results we are also ready, we are prepared at the beginning of the show_result.xml is to serve it, and now we have a separate activity, named Resultactivity.
What this resultactivity needs to do is simple:
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (R.layout.show_result);
Mwebview = (webview) Findviewbyid (r.id.wv_result);
Intent in = Getintent ();
String message = In.getstringextra (myactivity.extra_message);
Mwebview.loadurl ("http://www.baidu.com.cn/s?wd=" + message);
}
Also, we complete the webview binding in the OnCreate function and get the Extra_message value from the intent in the last activity (i.e. myactivity), and call the Baidu interface and open it.
For example, search for Ourunix, and click on the Search button will appear the following page:
In this article I did not overemphasize the lifecycle of the activity of one of the four components of Android and the intent of components and components, and this article mainly allows beginners to get started with Android as quickly as possible and experience the simplicity of Android development, About the lifecycle of the activity and other components I'll explain in the next one by one.