SoFunction
Updated on 2025-03-11

Android Development Self-study Notes (V): Use the Code Control Interface

The cool appearance is already in place, so let us start to realize its functions. It is strong on the outside and hard on the inside. It is not possible to have a flirting skill. We need real skills and a heart that can never stop self-improvement. We often think about what our dreams are so that we will not lose ourselves and find our way forward in the vast world! I won't tell you that I just watched "voice of China session 3".

From the previous two tutorials, we have already made the following beautiful layout. Welcome to turn the page and check it out:)

Android Self-study Development Chapter 4 Layout
Android Self-study Development Chapter 5 Layout

Add a second layout
We will not repeat the layout of the layout. Here we add an additional layout page to display search results. We still select LinearLayout. This layout is also an instantiation of the ViewGroup. We add a View to display the results. This View is a WebView.

<LinearLayout xmlns:andro
 xmlns:tools="/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal">
 <WebView android:
 android:layout_width="match_parent"
 android:layout_height="match_parent" />
</LinearLayout>

And define the id of the WebView as wv_result, OK. Let's call this layout show_result.xml, this page is so simple.

Add MyActivity code
The next good show is coming. Let’s take a look at the file structure first, otherwise I can’t stand it even if I talk about it on paper.

This MyActivity was modified from the Activity generated by default in creating this HelloAndroid project. It doesn’t matter what its name is. Let’s open it first. Do you remember that we once bound the search layout to this Activity? Let me briefly introduce that each Activity has multiple callback functions (i.e. onXXX series functions that start on), including onCreate, onStart, onResume, onStop, etc. These functions describe the life cycle of this Activity. This is very important. I will make a review for myself in detail. What we need to do is to add our own code in onCreate. This function is run as soon as MyActivity is opened.

I plan to add three sentences of code to this onCreate function in MyActivity.

 

We temporarily understand setContentView as a binding function, and we bind the search layout.

setup is a function I created myself. Its main function is to bind and initialize the various views of the search layout and bind listening events for them.

getResult is also created for me, mainly used to pass click event information and obtain search results.

 @Override
 public void onCreate(Bundle savedInstanceState) {
   (savedInstanceState);
   setContentView();
   setup();
   getResult();
 }

The setup function is as follows

 public void setup() {
   mSearchBtn = (Button) findViewById(.btn_search);
   mEnterEditText = (EditText) findViewById(.edit_message);
 }

 

This setup is very simple and gets the search button and search box in search.

getResult function is as follows

 public void getResult() {
   (new () {
     @Override
     public void onClick(View view) {
       //To change body of implemented methods use File | Settings | File Templates.
       String enterContent = ().toString();
       Intent in = new Intent(, );
       (EXTRA_MESSAGE, enterContent);
       startActivity(in);
     }
   });
 }

The getResult function binds the Listener (X monitor) to the search button. Once the user clicks this search button, we move to get the content of the search box and encapsulate it into EXTRA_MESSAGE, then attach this EXTRA_MESSAGE to the Intent, and use the startActivity function to call up the search result page.

Add search results code
We have also prepared the search results. The show_result.xml we prepared from the beginning is to serve it. Now we set another Activity, name it ResultActivity.

What this ResultActivity needs to do is simple:

@Override
 protected void onCreate(Bundle savedInstanceState) {
   (savedInstanceState);
   setContentView(.show_result);
 
   mWebView = (WebView)findViewById(.wv_result);
   Intent in = getIntent();
   String message = (MyActivity.EXTRA_MESSAGE);
   ("/s?wd=" + message);
 }

Similarly, we complete the binding of the WebView in the onCreate function, and get the value of EXTRA_MESSAGE from the Intent in the previous Activity (i.e. MyActivity), and call Baidu's interface and open it.


For example, if you search for ourunix and click the search button, the following page will appear:

In this article, I did not overemphasize the life cycle of the Activity, one of the four major components of Android, and the intent of the components and components bonding. This article mainly allows beginners to get started with Android as soon as possible and experience the simplicity of Android development. I will explain the life cycle of Activity and other components one by one later.