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

    三、运行结果

  • 相关阅读:
    在WinForm中通过C#调用CHM帮助文件
    筹备婚礼之音乐
    挖掘Microsoft Visual Studio 里面的资源之数据库连接配置
    好的Sql语句也能提高效率
    任务安排
    (读书笔记)
    MicroTeam 博客书写注意事项
    泛海精灵的用户分析:补充【Song Xie】
    什么是测试
    [scrum]2010.12.23
  • 原文地址:https://www.cnblogs.com/xiaoqing-ing/p/13095838.html
Copyright © 2011-2022 走看看