下面介绍Button事件实现的两种方法

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello"
  11. />
  12. <Button
  13. android:id="@+id/myButton1"
  14. android:text=" 按钮1 "
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. />
  18. <Button
  19. android:id="@+id/myButton2"
  20. android:text=" 按钮2 "
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. />
  24. </LinearLayout>

strings.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello">Hello World, ButtonDemoActivity!</string>
  4. <string name="app_name">ButtonDemo</string>
  5. </resources>

第一种:

ButtonDemoActivity.java

  1. package com.android.ButtonDemo.activity;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Button;
  7. import android.widget.Toast;
  8. public class ButtonDemoActivity extends Activity {
  9. Button myButton1,myButton2;
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. myButton1=(Button)findViewById(R.id.myButton1);
  15. myButton2=(Button)findViewById(R.id.myButton2);
  16. //使用匿名类注册Button事件
  17. myButton1.setOnClickListener(new OnClickListener()
  18. {
  19. public void onClick(View v)
  20. {
  21. Toast.makeText(ButtonDemoActivity.this, "你点击了按钮1",Toast.LENGTH_LONG).show();
  22. }
  23. });
  24. myButton2.setOnClickListener(new OnClickListener()
  25. {
  26. public void onClick(View v)
  27. {
  28. Toast.makeText(ButtonDemoActivity.this, "你点击了按钮2",Toast.LENGTH_LONG).show();
  29. }
  30. });
  31. }
  32. }

第二种:

ButtonDemoActivity.java

  1. package com.android.ButtonDemo.activity;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Button;
  7. import android.widget.Toast;
  8. public class ButtonDemoActivity extends Activity {
  9. Button myButton1,myButton2;
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. myButton1=(Button)findViewById(R.id.myButton1);
  15. myButton2=(Button)findViewById(R.id.myButton2);
  16. myButton1.setOnClickListener(new ButtonClick());
  17. myButton2.setOnClickListener(new ButtonClick());
  18. }
  19. //创建一个类,来响应OnClickListener
  20. class ButtonClick implements OnClickListener
  21. {
  22. public void onClick(View v)
  23. {
  24. switch (v.getId()) {
  25. case R.id.myButton1:
  26. Toast.makeText(ButtonDemoActivity.this, "你点击了按钮1",Toast.LENGTH_LONG).show();
  27. break;
  28. case R.id.myButton2:
  29. Toast.makeText(ButtonDemoActivity.this, "你点击了按钮2",Toast.LENGTH_LONG).show();
  30. break;
  31. default:
  32. break;
  33. }
  34. }
  35. }
  36. }

Android开发之Button事件实现方法的总结的更多相关文章

  1. Android开发之onClick事件的实现

    算是从2015年开始学习android开发,目前把onClick的事件实现写下来,记录下,以备参考. 实现button的点击功能,让textView显示一行文字,最简单的onClick事件. 直接贴代 ...

  2. Android开发之SQLite的使用方法

    前言 SQLite是一种轻量级的小型数据库,虽然比较小,但是功能相对比较完善,一些常见的数据库基本功能也具有,在现在的嵌入式系统中使用该数据库的比较多,因为它占用系统资源很少.Android系统中也不 ...

  3. Android开发之onClick事件的三种写法

    package a.a; import android.app.Activity; import android.os.Bundle; import android.view.View; import ...

  4. Android开发之onClick事件的三种写法(转)

    package a.a; import android.app.Activity; import android.os.Bundle; import android.view.View; import ...

  5. Android开发之Touch事件分发机制

    原地址http://www.cnblogs.com/linjzong/p/4191891.html Touch事件分发中只有两个主角:ViewGroup和View.Activity的Touch事件事实 ...

  6. 【Android UI】Android开发之View的几种布局方式及实践

    引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理 ...

  7. Android 开发之旅:深入分析布局文件&又是“Hello World!”

    http://www.cnblogs.com/skynet/archive/2010/05/20/1740277.html 引言 上篇可以说是一个分水岭,它标志着我们从Android应用程序理论进入实 ...

  8. Android开发之InstanceState详解

    Android开发之InstanceState详解   本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...

  9. Android开发之Java必备基础

    Android开发之Java必备基础 Java类型系统 Java语言基础数据类型有两种:对象和基本类型(Primitives).Java通过强制使用静态类型来确保类型安全,要求每个变量在使用之前必须先 ...

随机推荐

  1. [18] 螺旋楼梯(Spiral Stairs)图形的生成算法

    顶点数据的生成 bool YfBuildSpiralStairsVertices ( Yreal radius, Yreal assistRadius, Yreal height, Yuint sli ...

  2. OTL使用指南

    1 OTL简介 OTL 是 Oracle, Odbcand DB2-CLI Template Library 的缩写,是一个C++编译中操控关系数据库的模板库,它目前几乎支持当前所有的各种主流数据库, ...

  3. 【手势识别】简介 GestureDetector ScaleGestureDetector

    2017-3-6 单点触摸手势识别器GestureDetector 当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等.一般情况下,我们可以通过View或Activ ...

  4. 九度OJ 打印日期 (模拟)

    题目1186:打印日期 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4284 解决:1483 题目描写叙述: 给出年分m和一年中的第n天,算出第n天是几月几号. 输入: 输入包含两个整数 ...

  5. LeetCode【7】.Reverse Integer--java实现

    Reverse Integer 题目要求:给定一个int 类型值,求值的反转,例如以下: Example1: x = 123, return 321      Example2: x = -123, ...

  6. Python源代码 -- C语言实现面向对象编程(基类&amp;派生类&amp;多态)

    背景 python是面向对象的解释性语言.然而python是通过C语言实现的,C语言怎么跟面向对象扯上了关系? C语言能够实现面向对象的性质? 原文链接:http://blog.csdn.net/or ...

  7. unity3d插件Daikon Forge GUI 中文教程-3-基础控件Button和Sprite的使用

    (游戏蛮牛首发)大家好我是孙广东.官网提供了专业的视频教程http://www.daikonforge.com/dfgui/tutorials/,只是是在youtube上.要观看是须要FQ的. 只是教 ...

  8. 理解进程调度时机跟踪分析进程调度与进程切换的过程(Linux)

    ----------------------------------------------------------------------------------- 理解进程调度时机跟踪分析进程调度 ...

  9. 【转载】Remote System Explorer Operation总是运行后台服务,卡死eclipse解决办法

    原来是eclipse后台进程在远程操作,就是右下角显示的“Remote System Explorer Operation”.折腾了半天,在Stack Overflow找到答案(源地址).把解决方案翻 ...

  10. mac 终端 使用 gnu coreutils 工具 ls 颜色显示

    mac 终端默认 ls 命令无颜色显示: 1: 使用 ls -G 可以显示基本颜色 2:使用 gnu coreutils 工具 mac 终端 使用 gnu coreutils 工具 ls 颜色显示 以 ...