Android开发教程:页面切换测试


软件平台:Windows 7 + Eclipse + SDK

设计思路:

两个页面:mian和ok,每个页面上有一个按键,点击则可以互相切换

源代码:

main.xml源代码:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     />  
  12. <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一页面 "></Button>  
  13. </LinearLayout>  

ok.xml源代码:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="match_parent"  
  5.   android:layout_height="match_parent">  
  6.     <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上一页面 " android:id="@+id/button1"></Button>  
  7. </LinearLayout>  

makechoice源代码:

  1. package com.makechoice;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7.   
  8. public class makechoice extends Activity  
  9. {  
  10.     /** Called when the activity is first created. */  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState)   
  13.     {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         Button btn = (Button)findViewById(R.id.button1);  
  17.         btn.setOnClickListener(new Button.OnClickListener()  
  18.         {  
  19.             @Override  
  20.             public void onClick(View v)  
  21.             {  
  22.                 jump2ok();  
  23.             }  
  24.         });  
  25.     }  
  26.       
  27.     //跳到ok页面   
  28.     public void jump2ok()  
  29.     {  
  30.         setContentView(R.layout.ok);  
  31.         //当有按键按下跳到main页面   
  32.         Button btn = (Button)findViewById(R.id.button1);  
  33.         btn.setOnClickListener(new Button.OnClickListener()  
  34.         {  
  35.             @Override  
  36.             public void onClick(View v)  
  37.             {  
  38.                 jump2main();  
  39.             }  
  40.         });  
  41.     }  
  42.       
  43.   //跳到main页面   
  44.     public void jump2main()  
  45.     {  
  46.         setContentView(R.layout.main);  
  47.         //当有按键按下跳到ok页面   
  48.         Button btn = (Button)findViewById(R.id.button1);  
  49.         btn.setOnClickListener(new Button.OnClickListener()  
  50.         {  
  51.             @Override  
  52.             public void onClick(View v)  
  53.             {  
  54.                 jump2ok();  
  55.             }  
  56.         });  
  57.     }  
  58. }  

运行效果图:

注意:

类R中存放的ID号为当前页面的ID号,所以findViewById函数捕获的控件也为当前页面的控件

相关内容