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. spring+mybatis+atomikos 实现JTA事务

    1. 选择哪种transaction manager?      在单数据源情况下,JDBC,Hibernate,ibatis等自带的 transaction manager已能用于处理事务.     ...

  2. [转载]RHEL-6启动时提示:“/usr/libexec/gconf-sanit

    原文地址:RHEL-6启动时提示:"/usr/libexec/gconf-sanity-check-2 exited with status 256"作者:huage 系统环境:R ...

  3. 使用Idea添加PYTHONPATH的一种方案

    工作中我们常常需要更改PYTHONPATH,为项目添加一些依赖. 而不同的项目依赖的PYTHONPATH是不一样的,这就导致项目之间的PYTHONPATH发生混乱. 另一方面,有的电脑上PYTHON2 ...

  4. SIGTERM等信号含义【转】

    主要是做sigterm 和sigkill区别的比较,之前的好多操作,在结束正在运行的一个程序的时候,常用kill,这个以后的注意,在sigterm不起作用的时候,再使用kill; 原文地址:http: ...

  5. ss is one another utility to investigate sockets(特适合大规模tcp链接)

    原创文章,转载请注明: 转载自系统技术非业余研究 本文链接地址: ss is one another utility to investigate sockets(特适合大规模tcp链接) 具体的可以 ...

  6. C语言open()函数:打开文件函数(转)

    相关函数:read, write, fcntl, close, link, stat, umask, unlink, fopen 头文件:#include <sys/types.h>   ...

  7. 防盗链之URL参数签名 总结

    一.概述 传统的 IP 禁用.referer 防盗链.User-Agent 防盗链.地区访问控制等防盗链措施已经无法完全满足用户要求,所以开发出URL参数签名方式来防盗链 二.实现 Token防盗链是 ...

  8. block(八)作用域

    //闭包 NSString* s =@"123"; void (^block)() = ^() { NSLog(@"%@",s); }; block();// ...

  9. Mac PATH你所需要了解的

    关于Mac环境变量你需要知道: 0.何为环境变量? 1.如何查看环境变量? 2.如何配置环境变量? 3.如何重置环境变量? 0.环境变量: 所谓环境变量,$PATH,应用于Linux,OS X,Uni ...

  10. Oracle 12C -- top-n查询新特性

    Oracle 12C -- top-n查询新特性在12C中,增加了一些新的特性.可以指定返回结果集的指定数量的行.或按照百分比返回行. SQL> select count(*) from emp ...