• Android按钮单击事件的五种实现方式


    1. 匿名内部类作为事件监听器

      public class MainActivity extends Activity {
      	
      	private Button button;
          
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              button = (Button) findViewById(R.id.button1);
              button.setOnClickListener(new OnClickListener() {
      			@Override
      			public void onClick(View v) {
      				System.out.println("匿名内部类作为事件监听器");
      			}
      		});
              
          }
          
      }
      
    2. 内部类作为事件监听器

      public class MainActivity extends Activity {
      	
      	private Button button;
      	
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              button = (Button) findViewById(R.id.button1);
              button.setOnClickListener(new ButtonOnClickListener());
          }
          
          class ButtonOnClickListener implements OnClickListener{
      		@Override
      		public void onClick(View v) {
      			System.out.println("内部类作为事件监听器");
      		}
          }
          
      }
      
    3. Activity本身作为事件监听器

      public class MainActivity extends Activity implements OnClickListener{
      	
      	private Button button;
      	
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              button = (Button) findViewById(R.id.button1);
              button.setOnClickListener(this);
          }
          
          @Override
          public void onClick(View v) {
          	System.out.println("Activity本身作为事件监听器");
          }
          
      }
      
    4. 外部类作为事件监听器

      public class MainActivity extends Activity {
      	
      	private Button button;
      	
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              button = (Button) findViewById(R.id.button1);
              button.setOnClickListener(new ButtonOnClickListener());
          }
          
      }
      class ButtonOnClickListener implements OnClickListener {
      	
      	@Override
      	public void onClick(View v) {
      		System.out.println("外部类作为事件监听器");
      	}
      	
      }
      
    5. 将事件处理方法直接绑定到标签

      <Button
                  android:id="@+id/button1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="点击事件"
                  android:onClick="onClick" />
      
      public class MainActivity extends Activity {
      	
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
          }
          
          public void onClick(View v){
          	System.out.println("将事件处理方法直接绑定到标签");
          }
          
      }
      
  • 相关阅读:
    iOS 数据持久化--CoreData
    iOS 数据持久化-- FMDB
    iOS数据持久化--数据库
    iOS数据持久化--归档
    iOS数据持久化--用户属性
    python爬坑记录
    Flutter 爬坑记录
    Web开发爬坑记录
    博客 新址: http://zhoushaoting.com/
    微信小程序开发技巧及填坑记录
  • 原文地址:https://www.cnblogs.com/skyzou/p/12442199.html
Copyright © 2020-2023  润新知