Android笔记——Button点击事件几种写法

news/2024/7/17 3:20:26 标签: 移动开发, java

Button点击事件:大概可以分为以下几种:

  1. 匿名内部类
  2. 定义内部类,实现OnClickListener接口
  3. 定义的构造方法
  4. 用Activity实现OnClickListener接口
  5. 指定Button的onClick的属性

 

首先我们简单地定义一个带Button的xml布局文件

  activity_main.xml:

<Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击事件" />

 

然后再写Java代码

  MainActivity.java:

 

1.匿名内部类

  

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        bt = (Button) findViewById(R.id.bt1);
        //1.匿名内部类
        bt.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.i("匿名内部类", "点击事件");
            }
        });
}

 

2.定义内部类,实现OnClickListener接口

  

public class MainActivity extends Activity{

    private Button bt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        bt = (Button) findViewById(R.id.bt);

        bt.setOnClickListener(new MyListener());
    }

        //定义内部类,实现OnClickListene接口
    class MyListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.i("定义内部类,实现OnClickListene接口", "点击事件");
        }
    }
}

 

3.定义的构造方法

  

public class MainActivity extends Activity{

    private Button bt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        bt = (Button) findViewById(R.id.bt);

        myListener();
    }

        //定义构造方法
        private void myListener() {
        // TODO Auto-generated method stub
        bt.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.i("定义构造方法", "点击事件");
            }
        });
    }
}

 

4.用Activity实现OnClickListener接口

  

public class MainActivity extends Activity implements OnClickListener {

    private Button bt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        bt = (Button) findViewById(R.id.bt);
        bt.setOnClickListener(this);
    }
  
  
  //用Activity实现OnClickListener接口
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.i("用Activity实现OnClickListener接口", "点击事件");
        }
    
}

 

5.指定Button的onClick的属性:

  先在layout文件中指定onClick属性,然后到Activity中实现这个onButtonClick方法

布局文件:

    <Button

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="点击事件" />

  Java代码:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
    }

    //5指定onClick属性方式
    public void click(View v) {
        // TODO Auto-generated method stub
        Log.i("指定onClick属性方式","点击事件"); 
    }
}

 

 

  另外,多个按钮点击事件的处理,以指定onClick属性方式为例,获取其资源id,通过资源id,可以判断用户点击了哪个按钮了。

  

布局文件:

 <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="点击事件1" />
    
       <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="点击事件2" />
     
       <Button
        android:id="@+id/bt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="点击事件3" />/>
    
    <Button
        android:id="@+id/bt4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="点击事件4" />

 

Java代码:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
    }

    //指定onClick属性方式
    //传人的View对象,就是用户按下的那个按钮对象
    public void click(View v) {
        // TODO Auto-generated method stub
        
        //获取组件的资源id
        int id = v.getId();
        switch (id) {
        case R.id.bt1:
            Log.i("指定onClick属性方式","bt1点击事件"); 
            break;
        case R.id.bt2:
            Log.i("指定onClick属性方式","bt2点击事件"); 
            break;
        case R.id.bt3:
            Log.i("指定onClick属性方式","bt3点击事件"); 
            break;
        case R.id.bt4:
            Log.i("指定onClick属性方式","bt4点击事件"); 
            break;

        default:
            break;
        }        
    }
}

 

效果如图:

 

 

转载于:https://www.cnblogs.com/McCa/p/5018851.html


http://www.niftyadmin.cn/n/1120430.html

相关文章

JAVA Web servlet

Servlet servlet是运行在服务端的java程序。是sun公司提供一套规范&#xff08;接口&#xff09; 他是用来处理客户端请求的&#xff0c;响应浏览器的动态资源他的本质就是Java代码 创建servlet 创建一个类&#xff0c;继承HttpServlet servlet配置 每创建一个servlet 就要配…

(二)springmvc+mybatis+dubbo+zookeeper分布式架构 整合 - 平台功能导图

构建dubbo分布式平台的技术选型、目标、特点、独立服务项目等&#xff0c;今天针对于独立服务项目提供平台功能导图&#xff0c;也是我们未来逐步研发的功能。 架构代码下载&#xff1a; 我这边不做多介绍&#xff0c;直接上图了&#xff1a;下面的章节中&#xff0c;我们会针对…

大数据全面应用打击线下线上假冒伪劣

浙江省工商局与阿里巴巴集团日前签订深化战略合作协议&#xff0c;通过对网络经营主体实现精准监管等举措&#xff0c;把打击线下线上假冒伪劣推进到大数据全面应用阶段。 通过“工商阿里大数据交互平台”&#xff0c;浙江工商部门对阿里平台上登记的浙江网店主体身份信息与工商…

悲观锁和乐观锁和gap锁

mysql说对数据加锁不管共享锁还是互斥锁就能解决幻读的问题悲观锁&#xff1a;读写的时候都加锁&#xff0c;读取数据时给加锁&#xff0c;其它事务无法修改这些数据。修改删除数据时也要加锁&#xff0c;其它事务无法读取这些数据&#xff0c;serializable串行化隔离级别乐观锁…

Mysql JDBC连接数据库的一些语法

1、注册数据库 Class.forName(“Driver地址”) 2、建立和数据的连接 有三要素&#xff1a;地址、用户名、密码 Connection 名 DriverManager.getConnection(地址&#xff0c;用户名&#xff0c;密码) 3、获取执行SQL语句对象 Statement 名 Connection的名.createStatement() …

OCP-1Z0-051 第160题 insert中使用子查询

一、原题 View the Exhibit and examine the structure of ORDERS and CUSTOMERS tables. There is only one customer with the cust_last_name column having value Roberts. Which INSERT statement should be used to add a row into the ORDERS table for the customer …

光伏产业发展向好 市场规模料超万亿

第12届中国&#xff08;济南&#xff09;太阳能利用大会日前在山东省济南市举行。与会专家和从业者认为&#xff0c;中国光伏产业正步入健康发展阶段。业内人士预计&#xff0c;光伏发电潜在市场应用规模将超万亿元。 中国光伏行业协会秘书长王勃华认为&#xff0c;光伏制造业的…

也谈IO模型

目录 前言IO模型网络编程模型参考资料前言 说到IO模型&#xff0c;都会牵扯到同步、异步、阻塞、非阻塞这几个词。从词的表面上看&#xff0c;很多人都觉得很容易理解。但是细细一想&#xff0c;却总会发现有点摸不着头脑。自己也曾被这几个词弄的迷迷糊糊的&#xff0c;每次看…