Android Switch Page--setcontentview

Source: Internet
Author: User

Setcontentview

General Switch page, through the intent,startactivity can be achieved, but the system to create activity is very time-consuming, if the switch screen time requirements, can only use Setcontentview in the same activity under the switch page, so switch fast, But it will increase the difficulty of logic control of the program. In industrial products, any one of the parameters is to participate in the competition chips, boot time, screen switching time, etc. are important measures. Generally switch the time of the page in more than 150ms, the user will have a reaction, that is, the eyes can distinguish, to wait a little to see the page jump.

Activity code:

1  Packagecom.example.learntest;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.view.View;6 ImportAndroid.view.View.OnClickListener;7 ImportAndroid.widget.Button;8 ImportAndroid.widget.Toast;9 Ten  Public classMainactivityextendsActivity { One  A  LongTime = (Long) 0.0; -   Public voidonCreate (Bundle savedinstancestate) { -   Super. OnCreate (savedinstancestate);  the Setcontentview (r.layout.activity_main); -  -Button btn_mainactivity =(Button) Findviewbyid (r.id.btn_mainac_1); -Btn_mainactivity.setonclicklistener (NewButtonlisten ()); +  } -  +   A  classButtonlistenImplementsonclicklistener{ at    Public voidOnClick (View v) { -Time =System.currenttimemillis (); -    Switch(V.getid ()) { -         Caser.id.btn_mainac_1: - Jump2second (); -             Break; in         Caser.id.btn_seconda_1: - Jump2main (); to             Break; +        default: -             Break; the    } *   } $}//button Monitoring section, very easy to read and managePanax Notoginseng  -   Public voidJump2second () { the Setcontentview (r.layout.activity_second); +   LongCurrtime =System.currenttimemillis (); A  theToast.maketext (mainactivity. This, +"Switchover Time:" +string.valueof (currtime-time) + "MS", -  $ toast.length_short). Show (); $  -Button btn_secondactivity =(Button) Findviewbyid (r.id.btn_seconda_1); -Btn_secondactivity.setonclicklistener (NewButtonlisten ()); the}//jump to the second page method implementation - Wuyi   Public voidJump2main () { the Setcontentview (r.layout.activity_main); -   LongCurrtime =System.currenttimemillis (); Wu  -Toast.maketext (mainactivity. This, About"Switchover Time:" +string.valueof (currtime-time) + "MS", $  - toast.length_short). Show (); -Button btn_mainactivity =(Button) Findviewbyid (r.id.btn_mainac_1); -Btn_mainactivity.setonclicklistener (NewButtonlisten ()); A  } +}//return to the first page method implementation the    
View CodeDescription:

It is divided into four main parts:

1,OnCreate method, to set the monitoring, into the subsequent monitoring cycle

2, the monitoring method realization, judging is from which button, each button's r.id .... Are the only ones that are fixed.

3,Jump2second method Implementation, here first set the page as a second layout, and then to Findviewbyid again

Button btn_secondactivity = (button) Findviewbyid (r.id.btn_seconda_1);

Btn_secondactivity.setonclicklistener (New Buttonlisten ());
Set the listener again.
This point I am also more depressed, after the jump, have to find a bit. If you set these two buttons as global variables, or not! is not set to a static variable,

Jump page, be sure to Findviewbyid again, set up monitoring, in order to ensure the normal logic of the program.

4,the realization of Jump2main and similar. Do not repeat.

Android Switch Page--setcontentview

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.