本文转载自: http://zhangkun716717-126-com.iteye.com/blog/761080

前备知识: 1.需要了解怎么得到界面元素。

那么如何得到界面元素呢?在界面配置文件:例如 main.xml 中,比方一个id为idButtonTest1的Button定义如下:

<Button
android:id="@+id/idButtonTest1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_test1"
/>
<Button
android:id="@+id/idButtonTest2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_test2"
/>

在string常量配置文件string.xml中,配置如下常量

    <string name="button_test1">测试按钮1</string>
<string name="button_test2">测试按钮2</string>

那么得到该Button的做法就是findViewById(R.id.idButtonTest1); 比如:

Button buttonTest1 = (Button)findViewById(R.id.idButtonTest1);

buttonTest1就是那个id为idButtonTest1的Button了。

好了,下边开始今天的主题。今天讲的主要是OnClickListener,该类位置:import android.view.View.OnClickListener; 那么如何给刚才那个Button添加click事件呢?代码如下:

buttonTest1.setOnClickListener(newOnClickListener);

这里的newOnClickListener是一个OnClickListener对象:

private OnClickListener newOnClickListener = new OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(demo2.this, "Toast:Button_Test1", Toast.LENGTH_SHORT).show();
}
};

这是比较罗嗦,或者说是比较婆妈的做法,实际使用中我们可以简写为:

findViewById(R.id.idButtonTest1).setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(demo2.this, "Toast:Button_Test1", Toast.LENGTH_SHORT).show();
}
});

不过不推荐直接用findViewById(R.id.idButtonTest1),不规范呀么不规范~ 还有一种则是把全部的click事件给一个clickHandler来处理:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); Button buttonTest1 = (Button)findViewById(R.id.idButtonTest1);
Button buttonTest2 = (Button)findViewById(R.id.idButtonTest2);
buttonTest1.setOnClickListener(clickHandler);
buttonTest2.setOnClickListener(clickHandler);
} private OnClickListener clickHandler = new OnClickListener(){
@Override
public void onClick(View v) {
int id = v.getId();
switch(id){
case R.id.idButtonTest1:
Toast.makeText(demo2.this, "Toast:Button_Test1", Toast.LENGTH_SHORT).show();
break;
case R.id.idButtonTest2:
Toast.makeText(demo2.this, "Toast:Button_Test2", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(demo2.this, "Toast:none", Toast.LENGTH_SHORT).show();
}
}
};

Android SDK 2.0之后的版本提供了更简洁的方法,在Activity里创建一个public方法(记得设置View参数),然后在 Layout方法里直接设置。 逻辑和设计相比上边的简写和findViewById方法都更简单明了,更具可读性,也方便以后维护。

方式一:在配置中定义不同onClick事件方法

Activity中代码:

    public void myClickButton1(View v){
Toast.makeText(this, "Toast:Button_Test1", Toast.LENGTH_SHORT).show();
} public void myClickButton2(View v){
Toast.makeText(this, "Toast:Button_Test2", Toast.LENGTH_SHORT).show();
}

在main.xml中配置内容如下:

<Button
android:id="@+id/idButtonTest1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_test1"
android:onClick="myClickButton1"
/>
<Button
android:id="@+id/idButtonTest2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_test2"
android:onClick="myClickButton2"
/>

注意:main.xml中的配置android:onClick="myClickButton1"和android:onClick="myClickButton2",如果熟悉JavaScript的朋友就会发现,这和JavaScript的onclick事件定义方法一模一样啊! :-)

方式二:在配置中定义相同的onClick事件方法,然后在onClick方法中根据不同的按钮执行不同的操作。

Activity中代码:

public void myClickHandler(View v){
int id = v.getId();
switch(id){
case R.id.idButtonTest1:
Toast.makeText(demo2.this, "Toast1:Button_Test1", Toast.LENGTH_SHORT).show();
break;
case R.id.idButtonTest2:
Toast.makeText(demo2.this, "Toast2:Button_Test2", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(demo2.this, "Toast:none", Toast.LENGTH_SHORT).show();
}
}

main.xml配置内容如下:

<Button
android:id="@+id/idButtonTest1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_test1"
android:onClick="myClickHandler"
/>
<Button
android:id="@+id/idButtonTest2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_test2"
android:onClick="myClickHandler"
/>

[转]android-学习笔记之按钮事件的更多相关文章

  1. Android学习笔记点击事件和触摸事件的区别

    当我们点击手机屏幕的时候Android系统不仅会触发单击事件,还会触发触摸事件.在Android中它会先触发触摸事件,如果这个触摸事件没有被消费掉再去触发单击事件 代码示例: MainActivty. ...

  2. Android学习笔记-Button(按钮)

    Button是TextView的子类,所以TextView上很多属性也可以应用到Button 上!我们实际开发中对于Button的,无非是对按钮的几个状态做相应的操作,比如:按钮按下的时候 用一种颜色 ...

  3. Android学习笔记长按事件的处理

    常见的长按事件 代码示例: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedIns ...

  4. Android学习笔记之Activity详解

    1 理解Activity Activity就是一个包含应用程序界面的窗口,是Android四大组件之一.一个应用程序可以包含零个或多个Activity.一个Activity的生命周期是指从屏幕上显示那 ...

  5. android学习笔记36——使用原始XML文件

    XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...

  6. Android学习笔记之JSON数据解析

    转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...

  7. 【转】 Pro Android学习笔记(七五):HTTP服务(9):DownloadManager

    目录(?)[-] 小例子 保存在哪里下载文件信息设置和读取 查看下载状态和取消下载 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件,转载须注明出处:http://blog.csd ...

  8. 【转】 Pro Android学习笔记(五五):调试和分析(3):adb命令、模拟器控制台和StrictMode

    目录(?)[-] adb命令 模拟器Console StrictMode adb命令 我们在学习SQLite的使用,介绍过部分adb命令的使用,见Pro Android学习笔记(五):了解Conten ...

  9. 【转】 Pro Android学习笔记(五六):配置变化

    目录(?)[-] Activity的destorycreate过程 Fragment的destorycreate过程 onSaveInstanceState saveFragmentInstanceS ...

  10. 【转】 Pro Android学习笔记(三五):Menu(6):XML方式 & PopUp菜单

    目录(?)[-] 利用XML创建菜单 XML的有关属性 onClick事件 Pop-up菜单 利用XML创建菜单 在代码中对每个菜单项进行设置,繁琐且修改不灵活,不能适配多国语言的要求,可以利用资源进 ...

随机推荐

  1. BZOJ 1012 洛谷1198 最大数 maxnumber

    用线段数维护即可 #include<cstdio> #include<algorithm> #define ls (cur<<1) #define rs (cur& ...

  2. CodeForcesGym 100753E Change of Scenery

    Change of Scenery Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on CodeForc ...

  3. Regular Number 字符串匹配算法 Shift_and

    Using regular expression to define a numeric string is a very common thing. Generally, use the shape ...

  4. 用API中的raf复制文件图片等及系统找不到指定的文件的解决办法

    该运行是在eclipse中进行的操作,小白的基础理解,如有不妥之处,请大佬们指正.QQ:1055802635 package raf; import java.io.IOException;impor ...

  5. - > 贪心基础入门讲解一——完美字符串

    约翰认为字符串的完美度等于它里面所有字母的完美度之和.每个字母的完美度可以由你来分配,不同字母的完美度不同,分别对应一个1-26之间的整数. 约翰不在乎字母大小写.(也就是说字母F和f)的完美度相同. ...

  6. laravel5.5更新到laravel5.7

    为什么要更新呢?因为项目用的第三方后台扩展包,有很些bug,不够完美.想要一个漂亮的后台,那个后台只支持5.7. 然后,我就开始更新框架了. 修改后:"php": "&g ...

  7. iOS 打开扬声器以及插入耳机的操作

    废话不多说说一下现状 网上好多关于扬声器的操作,可是问题多多.SDK7.X 和SDK7.X以上版本号有点诧异 #import <Foundation/Foundation.h> #impo ...

  8. jQuery--编辑表格

    表格操作是我们常常遇到的,还记得刚開始学习牛腩新闻公布系统时.跟着视频进行表格的一些基本操作.而对它的原理与概念全然不懂,不过跟着老师的操作而进行操作. 通过这次学习,对表格的操作有了进一步的了解与掌 ...

  9. ubuntu多版本jdk安装及切换

    系统:ubuntu14.04 一.安装openjdk1.7 sudo apt-get install openjdk-7-jre openjdk-7-jdk 安装完成后找到其安装路径: dpkg -L ...

  10. YTU 2720: 删出多余的空格

    2720: 删出多余的空格 时间限制: 1 Sec  内存限制: 128 MB 提交: 338  解决: 201 题目描述 小平在给弟弟检查英语作业时时,发现每个英语句子单词之间的空格个数不等,请你编 ...