Android instances in simple output series-jump between mobile phone pages

Source: Internet
Author: User

On a webpage, we can jump from a webpage to another Webpage through a hyperlink. How can we achieve the jump between webpages on a mobile phone?
Principle: It is implemented through the combination of the layout file and the setContentView () method. Click the button in the first layout file main. xml to load main2.xml, and then click the button in the second layout file main2.xml to load the first layout file main. xml.

1.1: The first layout file main. xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Jump to the second mobile page"
Android: id = "@ + id/btn1"
/>
</LinearLayout>

1.2: Second layout file main2.xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello2"
/>
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Jump to the first mobile page"
Android: id = "@ + id/btn2"
/>
</LinearLayout>
1.3: character file stings. xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> I am the first mobile phone layout page </string>
<String name = "hello2"> I am the second mobile phone layout page </string>
<String name = "app_name"> setContentViewDemo </string>
</Resources>
1.4: code file

Package com. menglin. setcontentview;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;

Public class MainActivity extends Activity
{
Private Button btn1 = null;
Private Button btn2 = null;
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
// The first layout file is loaded by default.
SetContentView (R. layout. main );
// Use the findViewById () method to obtain the Button object in the first layout file.
Btn1 = (Button) findViewById (R. id. btn1 );
// Bind a listener to the Button object
Btn1.setOnClickListener (new Button1Listener ());
}
 
// The Listener of the button in the first layout File
Private class Button1Listener implements OnClickListener
{
@ Override
Public void onClick (View v)
{
// Load the second layout File
SetContentView (R. layout. main2 );
// Use findViewById () to obtain the Button object in the second layout File
Btn2 = (Button) findViewById (R. id. btn2 );
// Bind a listener to the Button object
Btn2.setOnClickListener (new Button2Listener ());
}
}
 
// Listener of the button in the second layout File
Private class Button2Listener implements OnClickListener
{
@ Override
Public void onClick (View v)
{
// Load the first layout File
SetContentView (R. layout. main );
// Use the findViewById () method to obtain the Button object in the first layout file.
Btn1 = (Button) findViewById (R. id. btn1 );
// Bind a listener to the Button object
Btn1.setOnClickListener (new Button1Listener ());
}
}
}

Note: although the interface is redirected back and forth, it is always the same Activity, so class variables and functions are all public.

 

The running effect is as follows:

When we click the button in the first layout file, it will switch to the second layout file we designed.

  

Related Article

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.