Android 在XML文件中添加View点击事件的监听:OnClickListener
2011-09-22 11:27
591 查看
在 SDK1.6 之后,Android就支持在XML文件中直接设置View点击时间的监听,这样又能少写一些代码咯,还能统一管理点击事件!!
下面是Android文档的说明:
Since:
API Level
Name 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 specify
you must declare a
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"
a value of this type.
This corresponds to the global attribute resource symbolonClick.
看下具体的实现:
1.main.xml文件
2. MyOnClickListener.java文件
下面是Android文档的说明:
XML Attributes
android:onClickSince:
API Level
Name 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 specify
android: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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android: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 技术之为RecyclerView添加item的点击事件
- Android中WebView加载Html中的图片添加点击事件
- android 为TextView的部分文字设置超链接样式并监听点击事件
- Android:一个TextView分段改变样式、添加监听事件的实现方式(类似朋友圈评论)
- Android开发给TextView添加点击事件
- Android中为RecyclerView添加item的点击事件
- Android给TextView添加点击事件的实现方法
- Android TextView加载HTMl图文之添加点击事件和查看图片
- Android自定义View与添加点击事件
- Android 高级UI设计笔记20:RecyclerView 的详解之RecyclerView添加Item点击事件
- Android开发给TextView添加点击事件
- Android RecyclerView (三)初学,为RecyclerView添加Item点击监听。
- 给RecyclerView的item添加点击监听事件
- 给RecyclerView的item添加点击监听事件
- Android TextView 高亮字体并添加点击事件
- android recyclerview的使用,添加分割线,点击,长按事件
- Android TextView 部分文字变色添加点击事件
- Android RecyclerView的使用与添加点击事件
- Android edittext中添加图片并且监听图片的点击事件
- Android中Recyclerview使用6----添加条目得到点击事件和长按事件(另一种写法,较简单)