1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.view.View.OnClickListener;
  5. import android.widget.Button;
  6. import android.widget.Toast;
  7. public class HelloActivity extends Activity {
  8. /** Called when the activity is first created. */
  9. @Override
  10. public void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.main);
  13. Button btnMethod01;
  14. Button btnMethod02;
  15. Button btnMethod03;
  16. btnMethod01 = (Button)findViewById(R.id.button1);
  17. btnMethod02 = (Button)findViewById(R.id.button2);
  18. btnMethod03 = (Button)findViewById(R.id.button3);
  19. //第一种方法:匿名类
  20. btnMethod01.setOnClickListener(new Button.OnClickListener(){
  21. public void onClick(View v){
  22. Toast.makeText(HelloActivity.this,R.string.method01
  23. ,Toast.LENGTH_SHORT).show();
  24. }
  25. });
  26. //添加监听事件
  27. btnMethod02.setOnClickListener(new button1OnClickListener());
  28. }
  29. //第二种方法:内部类实现  有两部 1.写内部类 2.添加监听事件
  30. private class button1OnClickListener implements OnClickListener{
  31. public void onClick(View v){
  32. Toast.makeText(HelloActivity.this,R.string.method02,
  33. Toast.LENGTH_SHORT).show();
  34. }
  35. }
  36. //第三种方法:用xml方法配置,该名称要与  main.xml 中button03的
  37. //android:onClick="OnClickButton03"的名字一样
  38. public void OnClickButton03(View v){
  39. Toast.makeText(HelloActivity.this,R.string.method03
  40. ,Toast.LENGTH_SHORT).show();
  41. }
  42. }
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class HelloActivity extends Activity { /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnMethod01;
Button btnMethod02;
Button btnMethod03; btnMethod01 = (Button)findViewById(R.id.button1);
btnMethod02 = (Button)findViewById(R.id.button2);
btnMethod03 = (Button)findViewById(R.id.button3); //第一种方法:匿名类
btnMethod01.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){
Toast.makeText(HelloActivity.this,R.string.method01
,Toast.LENGTH_SHORT).show();
} }); //添加监听事件
btnMethod02.setOnClickListener(new button1OnClickListener());
}
//第二种方法:内部类实现 有两部 1.写内部类 2.添加监听事件
private class button1OnClickListener implements OnClickListener{
public void onClick(View v){
Toast.makeText(HelloActivity.this,R.string.method02,
Toast.LENGTH_SHORT).show();
} }
//第三种方法:用xml方法配置,该名称要与 main.xml 中button03的
//android:onClick="OnClickButton03"的名字一样
public void OnClickButton03(View v){
Toast.makeText(HelloActivity.this,R.string.method03
,Toast.LENGTH_SHORT).show();
} }
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <Button
  7. android:id="@+id/button1"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/method01"/>
  11. <Button
  12. android:id="@+id/button2"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:text="@string/method02"/>
  16. <Button
  17. android:onClick="OnClickButton03"
  18. android:id="@+id/button3"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:text="@string/method03"/>
  22. </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/method01"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/method02"/> <Button
android:onClick="OnClickButton03"
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/method03"/> </LinearLayout>

Android 开发添加控件事件的三种方式的更多相关文章

  1. CAD控件,CAD插件使用教程:Android开发使用控件--开发环境的搭建

    Android开发使用控件入门--环境搭建 2014-12-24 09:57     14人阅读     评论(0)     收藏         编辑     删除 CAD控件.CAD三维控件,手机 ...

  2. Android开发使用控件入门--环境搭建

    Android开发使用控件入门--环境搭建 软件名称(,梦,,想.CAD  ,控件) 1. 环境搭建: 3 1.1. 安装Eclipse 3 1.2. 下载JDK 3 1.3. 下载Android S ...

  3. 【VS开发】在VS2010中开发ActiveX控件设置测试容器的方式

    在VS2010中开发ActiveX控件设置测试容器的方式 借鉴文章http://blog.csdn.net/waxgourd0/article/details/7374669 在VS2010中开发MF ...

  4. 怎样在Android开发中FPS游戏实现的两种方式比较

    怎样在Android开发中FPS游戏实现的两种方式比较 如何用Android平台开发FPS游戏,其实现过程有哪些方法,这些方法又有哪些不同的地方呢?首先让我们先了解下什么是FPS 英文名:FPS (F ...

  5. Android Material Design控件学习(三)——使用TextInputLayout实现酷市场登录效果

    前言 前两次,我们学习了 Android Material Design控件学习(一)--TabLayout的用法 Android Material Design控件学习(二)--Navigation ...

  6. Android 触发Button按钮事件的三种方式

    1.新创建一个类 2.使用内部类 3.当多个button按钮时,为简化代码而创建的实例listener 贴代码: MainActivity.Java  文件: package com.android. ...

  7. 界面跳转+Android Studio Button事件的三种方式

    今天学习界面跳转 java类总是不能新建成功 看了网上教程 (20条消息) 关于android studio无法创建类或者接口问题的解决方法_qq_39916160的博客-CSDN博客 可以新建了 但 ...

  8. Android - 页面返回上一页面的三种方式

    今年刚刚跳槽到了新公司,也开始转型做Android,由此开始Android的学习历程. 最近在解很多UI的bug,在解bug过程中,总结了在UI的实现过程中,页面返回上一页面的几种实现方式. 一. 自 ...

  9. NGUI注册事件的三种方式

    1.第一种方式 当一个元素要执行某个方法,而这个方法在此元素赋予的脚本上有,那么直接会调用此方法,但此方法的名称必须是内置的固定名称,例如OnClick,OnMouseOver,OnMouseOut等 ...

随机推荐

  1. Kubernetes滚动更新介绍及使用-minReadySeconds

    滚动升级Deployment 现在我们将刚刚保存的yaml文件中的nginx镜像修改为 nginx:1.13.3,然后在spec下面添加滚动升级策略:   1 2 3 4 5 6 7 minReady ...

  2. ios中地图

    参考文章  http://blog.csdn.net/tangaowen/article/details/6527901 http://www.cnblogs.com/tangbinblog/arch ...

  3. CTF SQL注入知识点

    理解常用的登录判断 select * from user where username='admin' and password='123' 数据库元信息 infomation_schema 懂PHP ...

  4. django日志使用TimeRotateFileHandler

    如果使用django的settings.py设置日志会产生一些问题. 问题描述 报错信息如下: Traceback (most recent call last): File "C:\Pyt ...

  5. HDU 3584 Cube (三维数状数组)

    Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submi ...

  6. 转载:windiws server 2008R2 IIS7.5 设置win7 IIS7设置,文件夹权限配置,Authenticated Users,支持asp temp

    第一步,进入控制面板,点击程序图标 第二步,点击打开或关闭Windows功能 第三步,勾选Internet信息服务全部功能,或根据需要,点击确定,安装 第四步,进入控制面板,点击管理工具 第五步,点击 ...

  7. 【HTML】如何判断当前浏览器是否是IE

    HTML里: HTML代码中,在编写网页代码时,各种浏览器的兼容性是个必须考虑的问题,有些时候无法找到适合所有浏览器的写法,就只能写根据浏览器种类区别的代码,这时就要用到判断代码了.在HTML代码中, ...

  8. java 高性能读模式(译)

    原文地址:http://tutorials.jenkov.com/java-performance/read-patterns.html 好久没翻译了,逛知乎,无意间发现的一个链接,写的太好了,而且内 ...

  9. [MSP430] 对MSP430单片机__delay_cycles精确延时的说明及改正

    在这里, 我来讨论一下关于MSP430单片机使用__delay_cycles延时的问题. IAR for MSP430编译器提供了一个编译器内联的精确延时函数(并非真正的 函数)以提供用户精确延时使用 ...

  10. 【Qt】StackedWidget

    一个简单的堆栈窗体类: 左側列表框会出现不同的选项,右側显示所选的不同的窗口 #include<QHBoxLayout> #include "stackdlg.h" S ...