[转]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创建菜单 在代码中对每个菜单项进行设置,繁琐且修改不灵活,不能适配多国语言的要求,可以利用资源进 ...
随机推荐
- STM32 配置PC13~PC15
在STM32的数据手册的管脚分配图中可以看到:PC14与OSC32_IN公用一个引脚,PC15与OSC32_OUT公用一个引脚,它们的使用方法如下: 当LSE(低速外部时钟信号)开启时,这两个公用管脚 ...
- 洛谷 3285 [JLOI2014]松鼠的新家
[题解] 给出一条路径,问树上的点被经过了几次. 显然树剖之后树上差分就好了. #include<cstdio> #include<algorithm> #define N 3 ...
- Spring AOP 学习(五)
1. 使用动态代理实现AOP package com.atguigu.spring.aop; import java.lang.reflect.InvocationHandler; import ja ...
- hdu 1811拓扑排序+并查集(容器实现)
http://www.cnblogs.com/newpanderking/archive/2012/10/18/2729566.html #include<stdio.h> #includ ...
- 算(tyvjP4700)
背景 zhx和他的妹子出去玩. 描述
- [bzoj2038][2009国家集训队]小Z的袜子(hose)_莫队
小Z的袜子 hose 2009-国家集训队 bzoj-2038 题目大意:给定一个n个袜子的序列,每个袜子有一个颜色.m次询问:每次询问一段区间中每种颜色袜子个数的平方和. 注释:$1\le n,m\ ...
- 洛谷—— P2919 [USACO08NOV]守护农场Guarding the Farm
https://www.luogu.org/problem/show?pid=2919 题目描述 The farm has many hills upon which Farmer John woul ...
- spring-cloud-starter-hystrix(断路器)服务不通或者调用失败后的错误处理和回调
雪崩效应 在微服务架构中通常会有多个服务层调用,大量的微服务通过网络进行通信,从而支撑起整个系统.各个微服务之间也难免存在大量的依赖关系.然而任何服务都不是100%可用的,网络往往也是脆弱的,所以难免 ...
- Pivotal-tc-Server与Tomcat区别
Pivotal-tc-Server之前叫做SpringSource tc Server,包含三个版本分别是:Spring版.标准版和开发版,但其中只有开发版是免费的.比如在STS中包含的版本就是开发板 ...
- subclipse 和 eclipse结合遇到的问题
subclipse是eclipse的一个SVN插件.但是我在使用的时候不断的报出下面的错误: the applet is attempting to access the "exists&q ...