六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 37|回复: 0

如何在xml中直接添加view点击监听(参考rayleung博客)

[复制链接]

升级  35.2%

212

主题

212

主题

212

主题

进士

Rank: 4

积分
676
 楼主| 发表于 2013-1-15 02:25:52 | 显示全部楼层 |阅读模式
在Android1.6里面,添加Listener的工作变得相当的简单(感觉更像在做网页编程!),具体步骤如下:
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="Button01"       android:id="@+id/Button01"       android:layout_width="wrap_content"       android:layout_height="wrap_content"      android:      /> </LinearLayout>
2. TestOnClickListener.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;            }      }  }  
这种方法学习了,呵呵
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表