Android监听小实例


Android监听小实例,参考 Android基础教程第三版,修订版中内容。

相关下载:

Android基础教程(第3版 修订版).pdf 下载地址:

//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.   
  8.       
  9.     <LinearLayout   
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:orientation="vertical"  
  13.         android:layout_gravity="center"  
  14.         android:paddingLeft="10px"  
  15.         android:paddingRight="10px"  
  16.         >  
  17.         <TextView   
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:textSize="20px"  
  21.         android:layout_marginBottom="20dip"  
  22.         android:text="@string/text_lable"  
  23.         android:layout_gravity="center"  
  24.         />  
  25.         <TableLayout   
  26.             android:layout_width="wrap_content"  
  27.             android:layout_height="wrap_content"  
  28.             android:layout_gravity="center"  
  29.             >  
  30.             <TableRow>  
  31.                  
  32.                  <Button   
  33.                     android:id="@+id/continue_button"  
  34.                     android:text="@string/contiune_label"  
  35.                     />  
  36.                   <Button   
  37.                     android:id="@+id/newgame_button"  
  38.                     android:text="@string/newgame_label"  
  39.                     />  
  40.             </TableRow>  
  41.             <TableRow>  
  42.                 <Button   
  43.                     android:id="@+id/about_button"  
  44.                     android:text="@string/about_label"  
  45.                     />  
  46.                  <Button   
  47.                     android:id="@+id/exit_button"  
  48.                     android:text="@string/exit_label"  
  49.                     />  
  50.             </TableRow>      
  51.         </TableLayout>  
  52.     </LinearLayout>  
  53. </LinearLayout>  
//strings.xml字符文件
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string name="app_name">书上案例</string>  
  5.       
  6.     <string name="about_text">\  
  7. 如果你想显示成对话框的形式的话就在AndroidManifest.xml里面有个  
  8. android:name=\".About\"那个Activity中加上一句\n  
  9. android:theme=\"@android:style/Theme.Dialog\"\n  
  10.     </string>  
  11.       
  12.     <string name="about_title">About_Title</string>  
  13.     <string name="text_lable">Welcome to Game world</string>  
  14.           
  15.     <string name="about_label">About</string>  
  16.     <string name="contiune_label">Continue</string>  
  17.     <string name="newgame_label">New Game</string>  
  18.     <string name="exit_label">Exit</string>  
  19.       
  20.   
  21. </resources>  
//BookDemoAboutActivity.java代码
  1. package sn.len.bookdemoabout;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8.   
  9. public class BookDemoAboutActivity extends Activity implements OnClickListener  
  10. {  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState)   
  13.     {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         //通过findviewById()方法找个各个按钮对应的ID   
  17.         View continueButton=findViewById(R.id.continue_button);  
  18.         View newGameButton=findViewById(R.id.newgame_button);  
  19.         View aboutButton=findViewById(R.id.about_button);  
  20.         View exitButton=findViewById(R.id.exit_button);  
  21.           
  22.         //为各个按钮分别注册监听   
  23.         continueButton.setOnClickListener(this);  
  24.         newGameButton.setOnClickListener(this);  
  25.         aboutButton.setOnClickListener(this);  
  26.         exitButton.setOnClickListener(this);  
  27.     }  
  28.       
  29.       
  30.     @Override  
  31.     public void onClick(View v)  
  32.     {  
  33.         //给指定的Button添加自己的处理内容   
  34.         switch(v.getId())  
  35.         {  
  36.         case R.id.continue_button:break;  
  37.         case R.id.newgame_button:break;  
  38.         case R.id.about_button:Intent i=new Intent(this,About.class);startActivity(i);break;  
  39.         //解析new Intent(this,About.class) this作当前,About.cass做目标,通过当前找到目标(通过本类Activity可以打开About这个Activity)   
  40.         //this是指本类(BookDemoAboutActivity)对象  -->当前   
  41.         //About.class是指About这个类的路径com.sn.len.About  -->目标   
  42.         case R.id.exit_button:Intent exit=new Intent(this,Exit.class);startActivity(exit);break;  
  43.         }  
  44.     }  
  45. }  
//About.java代码
  1. package sn.len.bookdemoabout;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class About extends Activity   
  7. {  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState)   
  11.     {  
  12.         // TODO Auto-generated method stub   
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.about);//设置 显示指定的布局内容about.xml   
  15.     }  
  16.       
  17. }  

//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.       
  15.       
  16. </ScrollView>  

//Exit.java代码
  1. package sn.len.bookdemoabout;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class Exit extends Activity   
  7. {  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState)   
  11.     {  
  12.         // TODO Auto-generated method stub   
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.about);  
  15.     }  
  16.       
  17. }  
  • 1
  • 2
  • 下一页

相关内容