1)第一种,也是最长见的添加方法(一下都以Button为例)

Button btn = (Button) findViewById(R.id.myButton);
btn .setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do something
}
});

2)第二种,下面这个方法较前一种稍微简单了一些,允许多个Buttons共享一个Listener。通过Switch控制对不同Button Click事件的响应方法:

Button btn = (Button) findViewById(R.id.mybutton);
Button btn2 = (Button) findViewById(R.id.mybutton2);
btn.setOnClickListener(handler);
btn2.setOnClickListener(handler);
View.OnClickListener handler = View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.mybutton:
//do something
break;
case R.id.mybutton2:
//do something
break;
}
}

或者

    Button list=null;
Button about=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
list=(Button)findViewById(R.id.foodlistbtn);
about=(Button)findViewById(R.id.aboutbutton);
// list.setText(text);
// list.setCompoundDrawables(left, top, right, bottom);
list.setOnClickListener(this);
about.setOnClickListener(this); }
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.foodlistbtn){
Intent intent=new Intent();
intent.setClass(MainApp.this, FoodListView.class);
startActivity(intent);
list.setBackgroundResource(R.drawable.btn_food_list_active);
}else if(v.getId()==R.id.aboutbutton){
Intent intent=new Intent(this, About.class);
startActivity(intent);
about.setBackgroundResource(R.drawable.btn_food_about_active);
}
}

3)第三种,直接将Clicklistener捆绑XML layout中的Views元素,在程序中定义的Listener方法需要带有一个View类型的参数:

<?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:id="@+id/text"
android:text="@string/hello" />
<Button android:id="@+id/mybutton" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:onClick="mybuttonlistener"></Button>
</LinearLayout>

java代码:

 Button btn = (Button) findViewById(R.id.mybutton);

 public void mybuttonlistener(View target){
//do something
}

从零开始学android开发-View的setOnClickListener的添加方法的更多相关文章

  1. 从零开始学android开发-详细谈谈intent的startActivityForResult()方法

    1.两种实现activity跳转的方法 实现activity的跳转主要有两种方法,startActivity()和startActivityForResult();例如activity A跳转到act ...

  2. 从零开始学android开发-用Intent启动Activity的方法

    启动另外一个Activity,可以有的方法有用setClass()和Component Name 1. 先说在setClass启动一个Activity的方法吧: Intent intent = new ...

  3. 从零开始学android开发- layout属性介绍

    android:id 为控件指定相应的ID android:text 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串 android:gravity 指定Vi ...

  4. View的setOnClickListener的添加方法

    1)第一种,也是最长见的添加方法(一下都以Button为例) 1 Button btn = (Button) findViewById(R.id.myButton);2 btn .setOnClick ...

  5. 从零开始学android开发-通过WebService进行网络编程,使用工具类轻松实现

    相信大家在平常的开发中,对网络的操作用到HTTP协议比较多,通过我们使用Get或者Post的方法调用一个数据接口,然后服务器给我们返回JSON格式的数据,我们解析JSON数据然后展现给用户,相信很多人 ...

  6. 从零开始学android开发-setBackgroundDrawable与setBackgroundResource的区别

    setBackgroundDrawable和setBackgroundResource的区别很多网友不知道View类提供的setBackgroundDrawable和setBackgroundReso ...

  7. 从零开始学android开发- 应用程序窗体显示状态操作requestWindowFeature

    我们在开发程序是经常会需要软件全屏显示.自定义标题(使用按钮等控件)和其他的需求,今天这一讲就是如何控制Android应用程序的窗体显示. 首先介绍一个重要方法那就是requestWindowFeat ...

  8. 从零开始学android开发-四大组件之一 Activity

    1.Activity是Android四大组件(Application Components)之一,简单来说Activity就是平常所见到的用户界面,一般情况下,一个Activity所占的窗口是满屏的, ...

  9. 从零开始学android开发-创建第一个android项目

    打开ADT开发工具

随机推荐

  1. 回调函数的应用误区4(c/s OK版本回调小程序)

    VC++深入详解里面说得也挺好:回调函数的实现机制: 1)定义一个回调函数 2)“函数实现者”(回调函数所在的模块)在初始化的时候,将回调函数的函数指针注册给“调用者”. 3)当特定的事件或条件发生的 ...

  2. [转] ArcGIS engine中气泡标注的添加、修改

    小生 原文 ArcGIS engine中气泡标注的添加.修改! 你微微地笑着,不同我说什么话.而我觉得,为了这个,我已等待得久了.                                   ...

  3. UE 使用技巧

    一.关于正则表达式的使用 删除空行: 替换 %[ ^t]++^p 为 空串 替换回车换行符:替换^p 为 空串 删除行尾空格: 替换 [ ^t]+$ 为 空串 删除行首空格: 替换 %[ ^t]+ 为 ...

  4. hdu1792 水题

    最近转到vim上来了,用vim编写代码,用gcc编译,用gdb调试.这是用vim做的第一道题,纪念下.题目很水,就不说了. /* * Author : ben */ #include <cstd ...

  5. IOS UIActivityIndicatorView 等待指示器

    自己做的一个 等待指示器 #import <UIKit/UIKit.h> @interface RockIndicatorView : UIView { } @property(nonat ...

  6. Multiple View Geometry in Computer Vision Second Edition by Richard Hartley 读书笔记(二)

    // Chapter 2介绍的是2d下的投影变换,摘录下了以下定理 Result 2.1. The point x lies on the line l if and only if xTl = 0. ...

  7. javascript AES加密 C#AES解密实现

    首先需要引入js类库 crypto-js(开源),地址:http://code.google.com/p/crypto-js 现在很多人无法打开这个地址不要紧,下面我们会将全部代码贴出来 需要引入 a ...

  8. 【暑假】[实用数据结构]UVa11995 I Can Guess the Data Structure!

    UVa11995  I Can Guess the Data Structure! 思路:边读边模拟,注意empty的判断! 代码如下: #include<iostream> #inclu ...

  9. well-posed problem and ill-posed problem

    well-posed problem must have the property that A solution exists The solution is unique The solution ...

  10. Ruby多字节字符的设计

    Perl.Python的多字节字符处理方式是UCS(Universal Code Set),Ruby的多字节字符处理方式是CSI(Code Set Independent).UCS的做法是,不管你读取 ...