Responding to Click Events
来源:互联网 发布:nba2k16捏脸数据大全 编辑:程序博客网 时间:2024/06/05 15:16
Responding to Click Events
When the user clicks a button, the Button
object receives an on-click event.
To define the click event handler for a button, add the android:onClick
attribute to the <Button>
element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event.The Activity
hosting the layout must then implement the corresponding method.
For example, here's a layout with a button using android:onClick
:
<?xml version="1.0" encoding="utf-8"?><Button xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/button_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_send" android:onClick="sendMessage" />
Within the Activity
that hosts this layout, the following method handles the click event:
/** Called when the user touches the button */public void sendMessage(View view) { // Do something in response to button click}
The method you declare in the android:onClick
attribute must have a signature exactly as shown above. Specifically, the method must:
- Be public
- Return void
- Define a
View
as its only parameter (this will be theView
that was clicked)
Using an OnClickListener
You can also declare the click event handler programmatically rather than in an XML layout. This might be necessary if you instantiate the Button
at runtime or you need to declare the click behavior in a Fragment
subclass.
To declare the event handler programmatically, create an View.OnClickListener
object and assign it to the button by calling setOnClickListener(View.OnClickListener)
. For example:
Button button = (Button) findViewById(R.id.button_send);button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Do something in response to button click }});
- Responding to Click Events
- [DirectShow] 035 - Responding to Events
- Responding to Touch Events 响应触摸事件
- Responding to resize events with Xlib
- UIViewController Class 参考2(Managing the View,Responding to View Events)
- Displaying Graphics with OpenGL ES (六)——Responding to Touch Events
- jQuery - Events事件 - click单击
- ExtJS Performance - Element click events
- adb not responding. if you'd like to retry then please manually kill adb.exe and click 'restart' 解决办
- ADB not responding. If you'd like to retry, then please manually kill “adb.exe” and click 'Restart'
- ADB not responding. If you'd like to retry, then please manually kill "adb.exe" and click 'Restart'
- 解决 adb not responding. if you'd like to retry then please manually kill adb.exe and click 'restart'
- ADB not responding. If you'd like to retry, then please manually kill "adb.exe" and click 'Restart'
- ADB not responding. If you'd like to retry, then please manually kill "adb.exe" and click 'Restart'”
- ADB not responding. If you'd like to retry, then please manually kill "adb.exe" and click 'Restart'
- ADB not responding. If you'd like to retry, then please manually kill "adb.exe" and click 'Restart'
- 6 click Events&A Basic Lifecycle Method
- 第一篇:解决ADB not responding. If you'd like to retry, then please manually kill "adb.exe" and click 'Res
- 作业一 目标二 从命令行输入一个参数(指定目录或文件),输出该目录下指定类型文件(.cs, .java)的个
- 2.1-1
- c# 进行ftp操作时,遇到‘#’特殊字符处理
- final知识点
- 已安装Framework4.0,仍提示没有安装Framework4.0问题
- Responding to Click Events
- 面向对象
- JAVA开发_发送邮件
- keepalvied安装配置过程
- 第一次作业
- 总结--正则表达式验证-显示数据
- 输出该文件的总行数、空行数、注释行数、代码行数
- java 第一次作业(修改版)
- linux上发邮件:mutt+msmtp