如何在xml中直接添加view点击监听
2010-07-14 10:33
113 查看
如何在xml中直接添加view点击监听(参考rayleung博客)
文章分类:JavaEye在Android1.6里面,添加Listener的工作变得相当的简单(感觉更像在做网页编程!),具体步骤如下:
1.main.xml文件
Java代码
<?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="Button01"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
/>
</LinearLayout>
<?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="Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="myClickHandler" /> </LinearLayout>
2. TestOnClickListener.java文件
Java代码
package com.ray.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class TestOnClickListener extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void myClickHandler(View target){
switch (target.getId()) {
case R.id.Button01:
setTitle("myButton01");
break;
}
}
}
相关文章推荐
- xml中不能直接添加ViewGroup吗
- 如何实现动态添加view并在其设置方法点击动态删掉
- 如何解决UIScrollView中添加UITapGestureRecognizer跟UIButton点击事
- AlertDialog中使用自定义View,其中的Button如何设置点击事件监听
- Android RecyclerView (三)初学,为RecyclerView添加Item点击监听。
- 给View 添加手势,点击无反应 如何给View添加点击事件,手势方法
- 如何解决UIScrollView中添加UITapGestureRecognizer跟UIButton点击事件冲突
- 为自定View添加相应属性,在XML可以直接使用
- 给RecyclerView的item添加点击监听事件
- Android学习笔记4 Imageview添加点击监听
- 如何给navigationView中的按钮添加点击事件
- 如何监听UIimageview的点击事件以及如何控制UIimageview显示圆形
- 如何用数据驱动实现通用化巡检APP(如何一个界面里添加多个点击拍照按钮并显示在不同的imageview里)
- UIScrollView,点击、滑动翻页事件的区分以及如何直接获取UIScrollView上的点击事件
- Android ViewPager放入多个XML。如何监听其的控件?
- Android ViewPager放入多个XML如何监听其的控件
- 在webview中如何监听页面的点击事件并跳转到指定的Activity?
- 给RecyclerView的item添加点击监听事件
- TextView如何给其中的某一段文字添加点击事件?
- Android 在XML文件中添加View点击事件的监听:OnClickListener