Android開發:三種事件處理模型

 一·接口實現事件處理模型

  1. public class MainActivity extends Activity implements OnClickListener 
  2.     private Button button1=null
  3.     private Button button2=null
  4.     private Button button3=null
  5.     private TextView text1=null
  6.      
  7.     @Override 
  8.     protected void onCreate(Bundle savedInstanceState) 
  9.     { 
  10.         super.onCreate(savedInstanceState); 
  11.         setContentView(R.layout.main01); 
  12.         button1=(Button) findViewById(R.id.button1); 
  13.         button2=(Button) findViewById(R.id.button2); 
  14.         button3=(Button) findViewById(R.id.button3); 
  15.         text1= (TextView) findViewById(R.id.textView1); 
  16.         button1.setOnClickListener(this); 
  17.         button2.setOnClickListener(this); 
  18.         button3.setOnClickListener(this); 
  19.          
  20.          
  21.     } 
  22.  
  23.     @Override 
  24.     public boolean onCreateOptionsMenu(Menu menu) { 
  25.         // Inflate the menu; this adds items to the action bar if it is present. 
  26.         getMenuInflater().inflate(R.menu.main, menu); 
  27.         return true
  28.     } 
  29.  
  30.     @Override 
  31.     public void onClick(View v)  
  32.     { 
  33.         // TODO Auto-generated method stub 
  34.         switch (v.getId()) 
  35.         { 
  36.         case R.id.button1: 
  37.             text1.setText("I am button one!"); 
  38.             break
  39.         case R.id.button2: 
  40.             text1.setText("I am button two!"); 
  41.             break
  42.         case R.id.button3: 
  43.             text1.setText("I am button three!"); 
  44.             break
  45.         } 
  46.          
  47.     } 
  48.  

二·內部類事件處理模型

 

  1. public class MainActivity extends Activity 
  2.     private Button button1=null
  3.     private Button button2=null
  4.     private Button button3=null
  5.     private TextView text1=null
  6.      
  7.     @Override 
  8.     protected void onCreate(Bundle savedInstanceState) 
  9.     { 
  10.         super.onCreate(savedInstanceState); 
  11.         setContentView(R.layout.main01); 
  12.         button1=(Button) findViewById(R.id.button1); 
  13.         button1.setOnClickListener(new Button1_OnClickListener() ); 
  14.         button2=(Button) findViewById(R.id.button2); 
  15.         button2.setOnClickListener(new Button2_OnClickListener() ); 
  16.         button3=(Button) findViewById(R.id.button3); 
  17.         button3.setOnClickListener(new Button3_OnClickListener() ); 
  18.         text1=(TextView) findViewById(R.id.textView1); 
  19.     } 
  20.     class Button1_OnClickListener implements OnClickListener 
  21.     { 
  22.          
  23.         @Override 
  24.         public void onClick(View arg0)  
  25.         { 
  26.             // TODO Auto-generated method stub 
  27.             text1.setText("I am the one!"); 
  28.              
  29.         } 
  30.          
  31.          
  32.     } 
  33.     class Button2_OnClickListener implements OnClickListener 
  34.     { 
  35.          
  36.         @Override 
  37.         public void onClick(View arg0)  
  38.         { 
  39.             // TODO Auto-generated method stub 
  40.             text1.setText("I am the two!"); 
  41.              
  42.         } 
  43.          
  44.          
  45.     } 
  46.     class Button3_OnClickListener implements OnClickListener 
  47.     { 
  48.          
  49.         @Override 
  50.         public void onClick(View arg0)  
  51.         { 
  52.             // TODO Auto-generated method stub 
  53.             text1.setText("I am the three!"); 
  54.              
  55.         } 
  56.          
  57.          
  58.     } 
  59.      
  60.     @Override 
  61.     public boolean onCreateOptionsMenu(Menu menu) { 
  62.         // Inflate the menu; this adds items to the action bar if it is present. 
  63.         getMenuInflater().inflate(R.menu.main, menu); 
  64.         return true
  65.     } 
  66.  

三·匿名類內部類處理模型

 

  1. package com.exam.test003; 
  2.  
  3.  
  4.  
  5. import android.os.Bundle; 
  6. import android.app.Activity; 
  7. import android.view.Menu; 
  8. import android.view.View; 
  9. import android.view.View.OnClickListener; 
  10. import android.widget.Button; 
  11. import android.widget.TextView; 
  12.  
  13. public class MainActivity extends Activity 
  14.     private Button button1=null
  15.     private Button button2=null
  16.     private Button button3=null
  17.     private TextView text1=null
  18.      
  19.     @Override 
  20.     protected void onCreate(Bundle savedInstanceState) 
  21.     { 
  22.         super.onCreate(savedInstanceState); 
  23.         setContentView(R.layout.main01); 
  24.         button1=(Button) findViewById(R.id.button1); 
  25.         button2=(Button) findViewById(R.id.button2); 
  26.         button3=(Button) findViewById(R.id.button3); 
  27.         text1= (TextView) findViewById(R.id.textView1); 
  28.         button1.setOnClickListener(new OnClickListener(){ 
  29.  
  30.             @Override 
  31.             public void onClick(View arg0)  
  32.             { 
  33.                 // TODO Auto-generated method stub 
  34.                 text1.setText("one"); 
  35.                  
  36.             }}); 
  37.         button2.setOnClickListener(new OnClickListener(){ 
  38.  
  39.             @Override 
  40.             public void onClick(View arg0) { 
  41.                 // TODO Auto-generated method stub 
  42.                 text1.setText("two"); 
  43.                  
  44.             }}); 
  45.         button3.setOnClickListener(new OnClickListener(){ 
  46.  
  47.             @Override 
  48.             public void onClick(View arg0)  
  49.             { 
  50.                 // TODO Auto-generated method stub 
  51.                 text1.setText("three"); 
  52.                  
  53.             }}); 
  54.     } 
  55.  
  56.     @Override 
  57.     public boolean onCreateOptionsMenu(Menu menu) { 
  58.         // Inflate the menu; this adds items to the action bar if it is present. 
  59.         getMenuInflater().inflate(R.menu.main, menu); 
  60.         return true
  61.     } 
  62.  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章