Android intermediate tutorial (1) -- Application of setcontentview for Mobile Page Conversion

Source: Internet
Author: User

Hello everyone, this section describes the conversion of mobile phone pages.Setcontentview
In the world of web pages, to switch between two pages, you only need to use hyperlinks,

But in the world of mobile phones, how can we convert mobile pages? The simplest way is to changeActivity
OfLayout
!

 

In this example, twoLayout
, RespectivelyLayout1 (main. XML)
AndLayout2 (mylayout. XML ),
DefaultLayout
IsMain. XML,
InLayout1
Create a button. When you click the button, the second button is displayed.Layout (mylayout. XML)
; Similarly, inLayout2
Also design a button, When you click the secondLayout
Is displayed back to the originalLayout1
To demonstrate how to switch between two pages.

 

First, let's take a look (to distinguish twoLayout
, We set different background colors respectively ):

 

 


 

 

The following is the Code involved in this program. The first is the layout of the main interface.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 = "Welcome to Wei zhulin's blog"
/>
<Button
Android: Id = "@ + ID/Bt1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "click to enter layout2"
/>


</Linearlayout>

 

Secondly, weMain. xml
Create a newMylayout. xml
File, the Code is as follows:

 

<? 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"
Android: Background = "# ffffffff"

>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "welcome to Mr Wei's blog"
/>
<Button
Android: Id = "@ + ID/bt2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "click to enter layout1"
/>


</Linearlayout>

 

Finally, our core programSetcontentviewdemo. Java

 

Package com. Android. setcontentviewdemo;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;

Import Android. widget. Button;

Public class setcontentviewdemo extends activity {

Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Load main. xml Layout
Setcontentview (R. layout. Main );


// Obtain the button object with findviewbyid () and add the event onclicklisener
Button Bt1 = (button) findviewbyid (R. Id. Bt1 );
Bt1.setonclicklistener (New button. onclicklistener (){
Public void onclick (view v ){

Gotolayout2 ();
}
});
}

// Switch layout from Main. XML to mylayout. xml
Public void gotolayout2 (){
// Change layout to mylayout.
Setcontentview (R. layout. mylayout );
Button b2 = (button) findviewbyid (R. Id. bt2 );
B2.setonclicklistener (New button. onclicklistener (){
Public void onclick (view v ){
Gotolayout1 ();
}
});
}

// Switch layout from mylayout. XML to main. xml
Public void gotolayout1 (){
Setcontentview (R. layout. Main );
Button Bt1 = (button) findviewbyid (R. Id. Bt1 );
Bt1.setonclicklistener (New button. onclicklistener (){
Public void onclick (view v ){
Gotolayout2 ();
}
});
}


}

 

Last executed !, This section ends now ~

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.