Andoid Button + Intent (suitable for beginners)
When I first learned Android, I spent a lot of time on it and did not find much information. To learn other people's "key code", write it by yourself. I hope that later students will spend less time on it.
In fact, it is very simple to use an Android Intent to implement the page jump of the Button. click the Button in figure 1 to enter figure 2.
Figure 1 <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140811/201408111019032.jpg" width = "100" height = "150" alt = "\">
Figure 2
First, create an Android Application project. The main class name is MainActivity. If the sdk is okay, the system will automatically generate the onCreate function. If the MainActivity inherits the Activity, it will import
There will be a Hello World! There is an xml file in the res layout folder. The lower version is main. xml, and the higher version is activity_main.xml,
Used to define the layout. .. Delete related items (used to display Hello World), add
In Graphical layout, You can first see the defined page, 1, and then write the layout of Figure 2. Create an e. xml file under the original directory layout and the layout is LinearLayout. The content is as follows:
Write an tt class corresponding to Figure 2
package com.example.buttontest;import android.app.Activity;import android.os.Bundle;public class tt extends Activity{protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.e); }}
The MainActivity content is as follows:
Package com. example. buttontest; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. widget. button; public class MainActivity extends Activity {Button but = null; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); but = (Button) findViewById (R. id. button1);. setOnClickListener (new View. onClickListener () {// listen to Buttonpublic void onClick (View arg0) {Intent intent = new Intent (); intent. setClass (MainActivity. this, tt. class); startActivity (intent );}});}}
You can add MainActivity. this. finish (); at the end of The onClick function. In addition, exit the entire system on Figure 2. If no value is added, the figure 1 page is displayed after figure 2 exits.
Finally, define the tt class in AndroidMainfest. xml, or enter the figure 2 interface in the simulator. The main class is generally defined by the system itself when it is created.