• 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("将事件处理方法直接绑定到标签");
          }
          
      }
      
  • 相关阅读:
    windows 10 上源码编译opengv | compile opengv on windows 10 from source
    ubuntu 16.04上源码编译libjpeg-turbo和使用教程 | compile and use libjpeg-turbo on ubuntu 16.04
    c 时间 学习
    cap文件的格式说明
    C#调用C++
    c/c++基础 const
    tomcat 简单安装配置说明
    言论
    近期任务
    解决mysql从windows迁移到centos出现乱码问题
  • 原文地址:https://www.cnblogs.com/skyzou/p/12442199.html
Copyright © 2020-2023  润新知