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