[转]android-学习笔记之按钮事件
本文转载自: 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-学习笔记之按钮事件的更多相关文章
- Android学习笔记点击事件和触摸事件的区别
当我们点击手机屏幕的时候Android系统不仅会触发单击事件,还会触发触摸事件.在Android中它会先触发触摸事件,如果这个触摸事件没有被消费掉再去触发单击事件 代码示例: MainActivty. ...
- Android学习笔记-Button(按钮)
Button是TextView的子类,所以TextView上很多属性也可以应用到Button 上!我们实际开发中对于Button的,无非是对按钮的几个状态做相应的操作,比如:按钮按下的时候 用一种颜色 ...
- Android学习笔记长按事件的处理
常见的长按事件 代码示例: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedIns ...
- Android学习笔记之Activity详解
1 理解Activity Activity就是一个包含应用程序界面的窗口,是Android四大组件之一.一个应用程序可以包含零个或多个Activity.一个Activity的生命周期是指从屏幕上显示那 ...
- android学习笔记36——使用原始XML文件
XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- 【转】 Pro Android学习笔记(七五):HTTP服务(9):DownloadManager
目录(?)[-] 小例子 保存在哪里下载文件信息设置和读取 查看下载状态和取消下载 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件,转载须注明出处:http://blog.csd ...
- 【转】 Pro Android学习笔记(五五):调试和分析(3):adb命令、模拟器控制台和StrictMode
目录(?)[-] adb命令 模拟器Console StrictMode adb命令 我们在学习SQLite的使用,介绍过部分adb命令的使用,见Pro Android学习笔记(五):了解Conten ...
- 【转】 Pro Android学习笔记(五六):配置变化
目录(?)[-] Activity的destorycreate过程 Fragment的destorycreate过程 onSaveInstanceState saveFragmentInstanceS ...
- 【转】 Pro Android学习笔记(三五):Menu(6):XML方式 & PopUp菜单
目录(?)[-] 利用XML创建菜单 XML的有关属性 onClick事件 Pop-up菜单 利用XML创建菜单 在代码中对每个菜单项进行设置,繁琐且修改不灵活,不能适配多国语言的要求,可以利用资源进 ...
随机推荐
- Python网络编程—socket(二)
http://www.cnblogs.com/phennry/p/5645369.html 接着上篇博客我们继续介绍socket网络编程,今天主要介绍的内容:IO多路复用.多线程.补充知识点. 一.I ...
- Shiro_DelegatingFilterProxy
1.DelegatingFilterProxy实际上是Filter的一个代理对象.默认情况下,Spring会到IOC容器中查找与<filter-name>对应的filter bean.也可 ...
- 通过注解配置Bean(2)
问:怎么用注解来配置bean与bean之间的引用关系? [组件装配] 1.<context:component-scan> 元素还会自动注册AutowiredAnnotationBeanP ...
- CodeForces 1000F One Occurrence
You are given an array $a$ consisting of $n$ integers, and $q$ queries to it. $i$-th query is denote ...
- HDU 1254 条件过程复杂的寻找最短路
这里一看就是找箱子到终点的最短路 一开始还傻傻的以为人的位置给的很没有意思- -,然后果然错了 没过多久想明白了错误,因为你推箱子并不是你想去哪里推就能去哪推的,首先得考虑人能否过的去,因为可能人被箱 ...
- RCC 2014 Warmup (Div. 2) 蛋疼解题总结
A. Elimination time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- redis sentinel集群配置及haproxy配置
ip分布情况: sentinel-1/redis 主 10.11.11.5 sentinel-2/redis 从 10.11.11.7 sentinel-3/redis 从 10.11.11.8 ha ...
- Maticsoft Code Generator
源码:https://github.com/easonjim/MaticsoftCodeGenerator bug提交:https://github.com/easonjim/MaticsoftCod ...
- 交换机是干嘛的!!交换机如何学习MAC地址过程?
1.它收到一个帧的时候,先检查源MAC地址,看看自己维护的一个地址表中有没有这个地址.如果有,则2:如果没有,则将这个MAC地址.进入的端口.进入的时间放入这个表中: 2.检查目的MAC地址,然后到该 ...
- cisco路由器上的DHCP
一.实验拓扑 二.具体配置 Router(config)#do sh run Building configuration... Current configuration : 604 bytes ...