本文以Button为例进行介绍

1》XML文件代码如下:

<Button    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/button_send"    android:onClick="sendMessage" />

The android:onClick attribute’s value, "sendMessage", is the name of a method in your activity that the system calls when the user clicks the button.

2》Open the Activity class (located in the project'ssrc/ directory) and add the corresponding method:

/** Called when the user clicks the Send button */public void sendMessage(View view) {    // Do something in response to button}


注意:

In order for the system to match this method to the method name given to android:onClick, the signature must be exactly as shown. Specifically, the method must:(该方法必须全部满足以下三个条件:)
•Be public.(public)
•Have a void return value.(返回值为void)
•Have a View as the only parameter (this will be the View that was clicked).(ps:有且仅有一个参数类型为View的参数,这一点特别重要,否则点击该按钮时,不会调用该方法。)


这也是有时候明明指定了android:onClick属性,并且Activity中也实现了对应的方法,但是实际执行的时候就是没有执行指定的方法的原因。仔细看一下你的方法是否同时满足以上三个条件!!!


更多相关文章

  1. Android(安卓)SwitchButton(滑动开关)
  2. Android乐动力V5.75最新获Key方法,提交步数,QQ登陆获取key案例
  3. Android(安卓)自定义Toast,不使用系统Toast
  4. 【Android】Zip文件解压方法
  5. Android显示GIF动画的几种方法
  6. Android中获取指定日期是星期几
  7. android 文件保存方法 sd卡中或系统
  8. 浅谈Java中Collections.sort对List排序的两种方法
  9. Python list sort方法的具体使用

随机推荐

  1. 针对网上流传的"Android(安卓)再按一次后
  2. Android学习小Demo(14)Android中关于PopupW
  3. Android布局中使用的@、@+、?和*的详细描述
  4. Android(安卓)TouchEvent 分发流程
  5. Android(安卓)surfaceflinger (4) -Vsync
  6. Android简易音乐播放器之界面实现(第一篇)
  7. 完全自定义Android对话框AlertDialog的实
  8. Android(安卓)ORM——初识greenDAO 3及使
  9. Android笔试和面试常见题目(一)
  10. Android(安卓)数据库框架LitePal使用详解