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();
    }
    });

    三、运行结果

  • 相关阅读:
    C#扩展方法
    asp.net mvc获取http body中的json
    ASP.NET MVC 获取表单数据
    @Html.DropDownList()的四种用法及自定义DropDownList扩展

    MVC5+EF6入门完整教程6:Partial View
    Day3.13组件切换
    Day3.12组件中的data和methods
    Day3.11定义私有组件
    Day3.10组件定义方式三
  • 原文地址:https://www.cnblogs.com/xiaoqing-ing/p/13095838.html
Copyright © 2011-2022 走看看