zoukankan      html  css  js  c++  java
  • Button控件的三种点击事件

    ①在布局文件中指定onClick属性的方法设置点击事件

     

     

    ②使用匿名内部类的方法设置点击事件

     

     

    ③实现Activity实现OnClickListen接口的方式设置点击事件

     

     

    linear.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/btn_one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="按钮1"
            />
        <Button
            android:id="@+id/btn_two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="按钮2"
            />
        <Button
            android:id="@+id/btn_three"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="按钮3"
            />
    
    </LinearLayout>

    MainActivity代码

    package com.iang.buttonclick;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        Button  btn_one,btn_two,btn_three;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.linear);
            btn_one=(Button)findViewById(R.id.btn_one);
           btn_two=(Button) findViewById(R.id.btn_two);
           btn_three=(Button) findViewById(R.id.btn_three);
    
    //       通话匿名类来监听鼠标点击事件
           btn_two.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   btn_two.setText("按钮2已经被点击");
               }
           });
    
           btn_three.setOnClickListener(this);
        }
    //    通过在xml文件里定义click来监听点击事件
        public void click(View view){
           btn_one.setText("按钮1已经被点击");
        }
    
    //    通过定义接口方法来监听鼠标点击事件
        @Override
        public void onClick(View v) {
            btn_three.setText("按钮3已经被点击");
        }
    }
  • 相关阅读:
    PHP 页面编码声明方法详解(header或meta)
    PHP error_reporting() 错误控制函数功能详解
    发送邮件程序报错454 Authentication failed以及POP3和SMTP简介
    使用 PHPMailer 发送邮件
    PHP debug_backtrace() 函数
    PHP_php.ini_说明详解
    详解spl_autoload_register()函数
    各浏览器对常用或者错误的 Content-Type 类型处理方式不一致
    string.format大全
    ASP.NET MVC如何实现自定义验证(服务端验证+客户端验证)
  • 原文地址:https://www.cnblogs.com/PerZhu/p/11599393.html
Copyright © 2011-2022 走看看