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

    三、运行结果

  • 相关阅读:
    hdu2476 string painter
    lightoj1422 Halloween Costumes
    cf1369D---找规律,递推
    cf1368D---贪心
    cf1373D---思维,最大子段和
    poj2279 Mr. Young's Picture Permutations
    AT2442 fohen phenomenon 差分
    poj2796 feel good 单调栈
    poj2082 terrible sets 单调栈
    洛谷P2979 cheese towers
  • 原文地址:https://www.cnblogs.com/xiaoqing-ing/p/13095838.html
Copyright © 2011-2022 走看看