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.