歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發教程:頁面切換測試

Android開發教程:頁面切換測試

日期:2017/3/1 11:12:13   编辑:Linux編程

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

運行效果圖:

注意:

類R中存放的ID號為當前頁面的ID號,所以findViewById函數捕獲的控件也為當前頁面的控件

Copyright © Linux教程網 All Rights Reserved