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. JS中文转换(UTF-8),url传递中文乱码解决

    来源于:http://blog.csdn.net/kuangfengbuyi/article/details/45557437 js合成url时,如果参数是中文,传到struts2中会乱码,解决办法如 ...

  2. Indy Changed from Indy10

    Indy Changed from Indy10 http://stackoverflow.com/questions/16339656/delphi-xe4-indy-compatibility-i ...

  3. Debug 路漫漫-05

    Debug 路漫漫-05: 1.使用这种方式计算 AUC 指标,结果出来居然是 NAN, —— 分母为(M*N),M或者N必有一个为0 了.(nan出现的情况绝大部分是分母出现0了)   若分子为0的 ...

  4. iOS 持续集成

    iOS 持续集成系列 - 开篇 前言 iOS 开发在经过这几年的野蛮生长之后,慢慢地趋于稳定.无论开发语言是 Objective-C 还是 Swift,工程类型是 Hybird 还是原生,开发思想是 ...

  5. NSObject协议中方法:description 和 debugDescription

    description基本概念 1.NSLog(@"%@", objectA);这会自动调用objectA的description方法来输出ObjectA的描述信息. 2.desc ...

  6. Xcode 8 的 Debug 新特性 —- WWDC 2016 Session 410 & 412 学习笔记

    Contents OverView Static Analyzer Localizability Instance Cleanup Nullablility Runtime Issue View De ...

  7. 树莓派SD卡制作,并成功启动

    需要条件: 硬件部分: 1.SD卡一张(树莓派支持的) 2.树莓派3 B + 一个(其他的版本也是可以的,这里默3B+). 1.下载树莓派镜像(NOOBS_v1_9_2.zip) wget http: ...

  8. Python学习笔记(八)—— 使用dict和set

    一.dict 1.定义: Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度 2.优势: di ...

  9. 解决UEditor将div标签换成p标签的问题

    原文链接 将设计排版好的页面html代码上传到数据库,再读取出来的时候发现所有的div都被替换成了p标签. 解决方法: 首先在ueditor.all.js文件内搜索allowDivTransToP,找 ...

  10. Linux安装ElasticSearch与MongoDB分布式集群环境下数据同步

    ElasticSearch有一个叫做river的插件式模块,可以将外部数据源中的数据导入elasticsearch并在上面建立索引.River在集群上是单例模式的,它被自动分配到一个节点上,当这个节点 ...