Android Study Notes (3): jump between activities

Source: Internet
Author: User

Create an Android project with the following code:

Main. xml


[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">"
<TextView android: id = "@ + id/textView"
Android: text = "click to Activity2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<Button android: id = "@ + id/button"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
 
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">"
<TextView android: id = "@ + id/textView"
Android: text = "click to Activity2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<Button android: id = "@ + id/button"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>

</LinearLayout> HelloWorldActivity. java


[Java] public class HelloWorldActivity extends Activity implements OnClickListener {
Private Button button;
/* Inherits the Activity and must implement the onCreate method.
* Every Activity must be registered in AndroidManifest. xml,
**/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button = (Button) findViewById (R. id. button );
Button. setText ("My Button ");
Button. setOnClickListener (this );
}
 
Public void onClick (View v ){
Intent intent = new Intent ();
Intent. setClass (HelloWorldActivity. this, Activity2.class );
// Start Activity2
HelloWorldActivity. this. startActivity (intent );
}
}
Public class HelloWorldActivity extends Activity implements OnClickListener {
Private Button button;
/* Inherits the Activity and must implement the onCreate method.
* Every Activity must be registered in AndroidManifest. xml,
**/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button = (Button) findViewById (R. id. button );
Button. setText ("My Button ");
Button. setOnClickListener (this );
}

Public void onClick (View v ){
Intent intent = new Intent ();
Intent. setClass (HelloWorldActivity. this, Activity2.class );
// Start Activity2
HelloWorldActivity. this. startActivity (intent );
}
}
Create a class Activity2:

Activity2.xml


[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<TextView android: id = "@ + id/textView"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "activy2"/>
 
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<TextView android: id = "@ + id/textView"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "activy2"/>

</LinearLayout> Activity2.java


[Java] public class Activity2 extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity2 );
}
}
Public class Activity2 extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity2 );
}
}
Register Acivity2 in AndroidManifest. xml:


[Html] <activity android: name = ". Activity2"
Android: label = "activity2"/>
<Activity android: name = ". Activity2"
Android: label = "activity2"/>
The running effect is as follows. If you press return, the system returns to HelloWorldActivity,


 


From Hu HU's column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.