Android 在XML文件中添加View点击事件的监听:OnClickListener
来源:互联网 发布:sketch中文破解版 mac 编辑:程序博客网 时间:2024/05/16 08:51
在 SDK1.6 之后,Android就支持在XML文件中直接设置View点击时间的监听,这样又能少写一些代码咯,还能统一管理点击事件!!
下面是Android文档的说明:
XML Attributes
android:onClickName of the method in this View's context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View. For instance, if you specifyandroid:onClick="sayHello"
, you must declare a public void sayHello(View v)
method of your context (typically, your Activity).
Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character.
This may also be a reference to a resource (in the form"@[package:]type:name
") ortheme attribute (in the form"?[package:][type:]name
")containing a value of this type.
This corresponds to the global attribute resource symbolonClick.
看下具体的实现:
1.main.xml文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/><Buttonandroid:text="ButtonTest"android:id="@+id/Button01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="myClickListener"/></LinearLayout>
2. MyOnClickListener.java文件
package com.ray.test;import android.app.Activity;import android.os.Bundle;import android.view.View;public class MyOnClickListener extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void myClickListener(View target){ switch (target.getId()) { case R.id.ButtonTest: setTitle("ButtonTest"); break; } } }
- Android 在XML文件中添加View点击事件的监听:OnClickListener
- 如何在xml中直接添加view点击监听
- android view点击事件无效 OnClickListener
- Android之事件监听的使用(OnClickListener)
- 不能添加OnClickListener监听事件
- Android开发中添加View控件的点击事件
- Android-在已有点击事件基础上添加点击监听
- android关于用.xml布局文件中对View添加OnClick事件无法响应的事情
- Android中按钮的点击事件监听
- Android中控件的onClickListener,点击监听器
- Android中控件的onClickListener,点击监听器
- 自定义点击事件OnClickListener内部监听外部回调adapter内的点击事件移到Activity
- Android edittext中添加图片并且监听图片的点击事件
- 在fragment中添加点击事件,给Linearlayout添加点击监听
- 详解android中OnClickListener监听
- android layout中添加XML以及监听事件放置的位置
- Android中父View和子view的点击事件
- Android中父View和子view的点击事件
- Android之用SharedPreferences来添加用户偏好
- RGBA颜色与兼容性的半透明背景色实例页面
- 在android输入框中禁止中文输入
- 简述Linux文件搜索
- RGBA颜色与兼容性的半透明背景色实例
- Android 在XML文件中添加View点击事件的监听:OnClickListener
- fedora15 + OpenCv2.2 + eclipse
- Java填充多边形相关API
- c++ 的4种类型转化方式
- git grep 效率实验
- c++ 三种继承(public protect private)
- Linux 文件系统概述
- 实例代码摘录_生产消费、死锁
- GRETA 用法