Multiple Ways to switch the layout of Android pages

Source: Internet
Author: User

 

Multiple page Switches

 

The assignment left by the teacher today is as follows: button switching, button switching, and touch switching.

 

First, let's talk about the logic of the first method:

 

First run the Code:

 

OneActivity. java file code:

 

 

 

Package cn. class3g;

 

 

 

Import android. app. Activity;

 

Import android. OS. Bundle;

 

Import android. view. View;

 

Import android. view. View. OnClickListener;

 

Import android. widget. Button;

 

 

 

Public class OneActivity extends Activity {

 

/** Called when the activity is first created .*/

 

@ Override

 

Public void onCreate (Bundle savedInstanceState ){

 

Super. onCreate (savedInstanceState );

 

SetContentView (R. layout. main );

 

 

Button nextButton = (Button) findViewById (R. id. next );

 

NextButton. setOnClickListener (new OnClickListener (){

 

 

@ Override

 

Publicvoid onClick (View v ){

 

SetContentView (R. layout. two );

 

ButtonupButton = (Button) findViewById (R. id. up );

 

UpButton. setOnClickListener (newOnClickListener (){

 

 

@ Override

 

Publicvoid onClick (View v ){

 

SetContentView (R. layout. main );

 

ButtonnextButton = (Button) findViewById (R. id. next );

 

 

}

 

});

 

}

 

});

 

 

}

 

}

 

Explanation: This is the code I first wrote. The layout file contains two types: main. xml and two. xml, two pages are displayed, each with a <TextView> and <Button> element. The Code is as follows:

 

Main. xml code:

 

<? Xml version = "1.0" encoding = "UTF-8"?>

 

<LinearLayoutxmlns: android = "http://schemas.android.com/apk/res/android"

 

Android: layout_width = "fill_parent"

 

Android: layout_height = "fill_parent"

 

Android: orientation = "vertical">

 

 

 

<TextView

 

Android: layout_width = "fill_parent"

 

Android: layout_height = "wrap_content"

 

Android: text = "This is the first page"

 

 

/>

 

<Button

 

Android: layout_width = "fill_parent"

 

Android: layout_height = "wrap_content"

 

Android: id = "@ + id/next"

 

Android: text = "next page"/>

 

 

</LinearLayout>

 

 

 

Two. xml code:

 

<? Xmlversion = "1.0" encoding = "UTF-8"?>

 

<LinearLayoutxmlns: android = "http://schemas.android.com/apk/res/android"

 

Android: layout_width = "fill_parent"

 

Android: layout_height = "fill_parent"

 

Android: orientation = "vertical">

 

 

 

<TextView

 

Android: layout_width = "fill_parent"

 

Android: layout_height = "wrap_content"

 

Android: text = "this is the second page"

 

/>

 

<Button

 

Android: layout_width = "fill_parent"

 

Android: layout_height = "wrap_content"

 

Android: id = "@ + id/up"

 

Android: text = "Previous Page"

 

/>

 

 

 

</LinearLayout>

 

After the two xml files are written, the logic is written. The java code is written as follows:

 

After the default onCreate method is initialized, the layout file main is called through setContentView. Xml is the default layout, and a Button component named nextButton is declared. The setOnClickListener listener is called to listen on the Button. In the listener, an onclickListener listener is added, A method named onClick is implemented in the listener. Here, I wrote a setContentView method to jump to the page, and clicked the Button to jump to two. the xml layout page opens two. xml page; on this page, write another Button named "upButton" to return to the page. In this way, the page switching effect is achieved by clicking the Button through nesting.

 

But this is not what I want. The effect I want is that no matter which button is clicked, different style pages will appear, so I can determine the button to achieve the effect. The first thought is to use the if statement to determine which button is clicked, and then perform different operations. I think this is better, but in fact it does not need to be so complicated to judge the key value:

 

 

 

Packagecn. class3g;

 

 

 

Importandroid. app. Activity;

 

Importandroid. OS. Bundle;

 

Importandroid. view. View;

 

Importandroid. widget. Button;

 

 

 

Public classActivityTest extends Activity {

 

/** Called when the activity is firstcreated .*/

 

@ Override

 

Public void onCreate (BundlesavedInstanceState ){

 

Super. onCreate (savedInstanceState );

 

SetContentView (R. layout. main );

 

 

Button nextButton = (Button) findViewById (R. id. next );

 

NextLayout (); // display the next page

 

}

 

 

 

Public void nextLayout (){

 

SetContentView (R. layout. two );

 

ButtonupButton = (Button) findViewById (R. id. up );

 

UpButton. setOnClickListener (newView. OnClickListener (){

 

 

 

@ Override

 

Public voidonClick (View v ){

 

UpLayout ();

 

 

}

 

});

 

}

 

 

Public void upLayout (){

 

SetContentView (R. layout. main );

 

ButtonnextButton = (Button) findViewById (R. id. next );

 

NextButton. setOnClickListener (newView. OnClickListener (){

 

 

@ Override

 

Public voidonClick (View v ){

 

NextLayout (); // display the next Desktop

 

 

}

 

});

 

}

 

}

 

 

 

Then, you can press the button to implement page switching. You can know through the teacher's morning instance that you can call the page layout file in the onClickDown or onClickUp method: setContentView (R. layout. main);, add the layout files of the two pages in OnClickDown and onClickUp respectively, and then add the opposite method to implement the return. The Code is as follows:

 

Public boolean onKeyDown (int keyCode, KeyEvent event ){

 

 

This. setContentView (R. layout. main );

 

 

Returnsuper. onKeyDown (keyCode, event );

 

}

 

 

Public boolean onKeyUp (int keyCode, KeyEvent event ){

 

// ShowInfo ("keyUp" + keyCode );

 

This. setContentView (R. layout. second );

 

Return super. onKeyUp (keyCode, event );

 

 

}

 

 

 

Touch implementation form www.2cto.com

 

Public booleanonTouchEvent (MotionEvent event ){

 

// ShowInfo ("ontouch: x =" + event. getX () + "y =" + event. getY ());

 

 

 

Flag =! Flag;

 

If (flag ){

 

This. setContentView (R. layout. main );

 

} Else {

 

This. setContentView (R. layout. second );

 

 

Returnsuper. onTouchEvent (event );

 

}

 

Summary:

 

Call the layout file: setContentView (R. layout. main );

 

Obtain the component ID: ButtonnextButton = (Button) findViewById (R. id. next );

 

A java file is renamed during the question preparation process and thus cannot be debugged because the <android: name> attribute value under <activity> In the AndroidManifest. xml file is forgotten.

 

From Anno'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.