zoukankan      html  css  js  c++  java
  • 4、点击事件

    第一种点击事件(不常用)

    一、布局代码

    <Button
    android:onClick="showToast"点击时间函数,需要去Activity中声明
    android:layout_below="@id/btn_3"
    android:id="@+id/btn_4"
    android:text="按钮4"
    android:textSize="30sp"
    android:textColor="#FFFFFF"
    android:layout_marginTop="20dp"
    android:background="@drawable/bg_btn4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

    二、Activity代码

    //    界面上弹出提示信息
    public void showToast(View view){注意showToast传递参数为View
    Toast.makeText(this,"我被点击了", LENGTH_SHORT).show();
    }

    三、实验结果

    第二种点击事件(常用)

    一、布局代码

    注意此时没有onClick事件
    <Button
    android:layout_below="@id/btn_2"
    android:layout_marginTop="20dp"
    android:id="@+id/tv_1"
    android:text="按钮3"
    android:textSize="30sp"
    android:textColor="#FF9900"
    android:background="@drawable/bg_btn3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

    二、Activity代码

    //声明一个button对象
    private Button mBtn3;

    mBtn3.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    Toast.makeText(ButtonActivity.this,"tv_1被点击了", LENGTH_SHORT).show();
    }
    });

    三、运行结果

  • 相关阅读:
    文件权限
    函数指针
    位操作
    宏定义
    GNU gcc常用编译选项
    SHELL编程(六)---------数组和函数
    26.使用中间件的方式包装日志输出
    25.最基本的日志输出方式,内置日志包使用
    24.把熔断器整合到我们的客户端代码中(初步)
    23.熔断器学习,熔断器的三种状态,状态获取
  • 原文地址:https://www.cnblogs.com/xiaoqing-ing/p/13095838.html
Copyright © 2011-2022 走看看