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("将事件处理方法直接绑定到标签");
        }
    
    }

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

上一篇:android获取数据库查询的结果


下一篇:Android修改左上角图标