歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android監聽小實例

Android監聽小實例

日期:2017/3/1 10:56:24   编辑:Linux編程

Android監聽小實例,參考 Android基礎教程第三版,修訂版中內容。

相關下載:

Android基礎教程(第3版 修訂版).pdf 下載地址:http://www.linuxidc.com/Linux/2011-11/48002.htm

//main.xml主頁面布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="horizontal"
  6. >
  7. <LinearLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:orientation="vertical"
  11. android:layout_gravity="center"
  12. android:paddingLeft="10px"
  13. android:paddingRight="10px"
  14. >
  15. <TextView
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:textSize="20px"
  19. android:layout_marginBottom="20dip"
  20. android:text="@string/text_lable"
  21. android:layout_gravity="center"
  22. />
  23. <TableLayout
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_gravity="center"
  27. >
  28. <TableRow>
  29. <Button
  30. android:id="@+id/continue_button"
  31. android:text="@string/contiune_label"
  32. />
  33. <Button
  34. android:id="@+id/newgame_button"
  35. android:text="@string/newgame_label"
  36. />
  37. </TableRow>
  38. <TableRow>
  39. <Button
  40. android:id="@+id/about_button"
  41. android:text="@string/about_label"
  42. />
  43. <Button
  44. android:id="@+id/exit_button"
  45. android:text="@string/exit_label"
  46. />
  47. </TableRow>
  48. </TableLayout>
  49. </LinearLayout>
  50. </LinearLayout>
//strings.xml字符文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="app_name">書上案例</string>
  4. <string name="about_text">\
  5. 如果你想顯示成對話框的形式的話就在AndroidManifest.xml裡面有個
  6. android:name=\".About\"那個Activity中加上一句\n
  7. android:theme=\"@android:style/Theme.Dialog\"\n
  8. </string>
  9. <string name="about_title">About_Title</string>
  10. <string name="text_lable">Welcome to Game world</string>
  11. <string name="about_label">About</string>
  12. <string name="contiune_label">Continue</string>
  13. <string name="newgame_label">New Game</string>
  14. <string name="exit_label">Exit</string>
  15. </resources>
//BookDemoAboutActivity.java代碼
  1. package sn.len.bookdemoabout;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. public class BookDemoAboutActivity extends Activity implements OnClickListener
  8. {
  9. @Override
  10. public void onCreate(Bundle savedInstanceState)
  11. {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. //通過findviewById()方法找個各個按鈕對應的ID
  15. View continueButton=findViewById(R.id.continue_button);
  16. View newGameButton=findViewById(R.id.newgame_button);
  17. View aboutButton=findViewById(R.id.about_button);
  18. View exitButton=findViewById(R.id.exit_button);
  19. //為各個按鈕分別注冊監聽
  20. continueButton.setOnClickListener(this);
  21. newGameButton.setOnClickListener(this);
  22. aboutButton.setOnClickListener(this);
  23. exitButton.setOnClickListener(this);
  24. }
  25. @Override
  26. public void onClick(View v)
  27. {
  28. //給指定的Button添加自己的處理內容
  29. switch(v.getId())
  30. {
  31. case R.id.continue_button:break;
  32. case R.id.newgame_button:break;
  33. case R.id.about_button:Intent i=new Intent(this,About.class);startActivity(i);break;
  34. //解析new Intent(this,About.class) this作當前,About.cass做目標,通過當前找到目標(通過本類Activity可以打開About這個Activity)
  35. //this是指本類(BookDemoAboutActivity)對象 -->當前
  36. //About.class是指About這個類的路徑com.sn.len.About -->目標
  37. case R.id.exit_button:Intent exit=new Intent(this,Exit.class);startActivity(exit);break;
  38. }
  39. }
  40. }
//About.java代碼
  1. package sn.len.bookdemoabout;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. public class About extends Activity
  5. {
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState)
  8. {
  9. // TODO Auto-generated method stub
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.about);//設置 顯示指定的布局內容about.xml
  12. }
  13. }

//about.xml文件

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ScrollView
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:padding="10dip"
  7. >
  8. <TextView
  9. android:id="@+id/about_content"
  10. android:layout_width="fill_parent"
  11. android:layout_height="fill_parent"
  12. android:text="@string/about_text"
  13. />
  14. </ScrollView>

//Exit.java代碼
  1. package sn.len.bookdemoabout;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. public class Exit extends Activity
  5. {
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState)
  8. {
  9. // TODO Auto-generated method stub
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.about);
  12. }
  13. }
Copyright © Linux教程網 All Rights Reserved