`
挞拔地瓜
  • 浏览: 2601 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

android view中如何加linstener,

 
阅读更多
在开发中为控件添加Listener是非常常见的工作,最简单的添加Listener方式可以这样:

Java代码
1.findViewById(R.id.myButton).setOnClickListener(new View.OnClickListener() {  
2.    public void onClick(View v) {  
3.        // Do stuff  
4.    }  
5.}); 
findViewById(R.id.myButton).setOnClickListener(new View.OnClickListener() {      public void onClick(View v) {          // Do stuff      }  }); 采用上述方法添加Listener有个缺点就是如果控件太多的话,Listener数量也会增多,因此,可以采用如下的小窍门减少Listener的数量:

Java代码
1.View.OnClickListener handler = View.OnClickListener() {  
2.    public void onClick(View v) {  
3.        switch (v.getId()) {  
4.            case R.id.Button01: // doStuff  
5.                break;  
6.            case R.id.Button02: // doStuff  
7.                break;  
8.        }  
9.    }  
10.}  
11. 
12.findViewById(R.id.myButton).setOnClickListener(handler);  
13.findViewById(R.id.myOtherButton).setOnClickListener(handler); 
View.OnClickListener handler = View.OnClickListener() {      public void onClick(View v) {          switch (v.getId()) {              case R.id.Button01: // doStuff                  break;              case R.id.Button02: // doStuff                  break;          }      }  }    findViewById(R.id.myButton).setOnClickListener(handler);  findViewById(R.id.myOtherButton).setOnClickListener(handler);在Android1.6里面,添加Listener的工作变得相当的简单(感觉更像在做网页编程!),具体步骤如下:

1.首先在layout里面定义Button并指定响应的Listener

Xml代码
1.<?xml version="1.0" encoding="utf-8"?> 
2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
3.    android:orientation="vertical" 
4.    android:layout_width="fill_parent" 
5.    android:layout_height="fill_parent" 
6.    > 
7.<TextView    
8.    android:layout_width="fill_parent"   
9.    android:layout_height="wrap_content"   
10.    android:text="@string/hello" 
11.    /> 
12.<Button   
13.    android:text="Button01"   
14.    android:id="@+id/Button01"   
15.    android:layout_width="wrap_content"   
16.    android:layout_height="wrap_content" 
17.    android:onClick="myClickHandler01" 
18.    /> 
19.<Button   
20.    android:text="Button02"   
21.    android:id="@+id/Button02"   
22.    android:layout_width="wrap_content"   
23.    android:layout_height="wrap_content" 
24.    android:onClick="myClickHandler02" 
25.    /> 
26.<TextView    
27.    android:layout_width="fill_parent"   
28.    android:layout_height="wrap_content"   
29.    android:text="@string/hello" 
30.    /> 
31.</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="myClickHandler01"   />  <Button    android:text="Button02"    android:id="@+id/Button02"    android:layout_width="wrap_content"    android:layout_height="wrap_content"   android:onClick="myClickHandler02"   />  <TextView        android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:text="@string/hello"      />  </LinearLayout>其中以下这两行就是新增的特性:

android:onClick="myClickHandler01"

android:onClick="myClickHandler02"



2.在活动里面定义public的方法myClickHandler01、和myClickHandler02(注意这两个方法必须有一个View的形参)。

Java代码
1.package com.ray.test;  
2. 
3.import android.app.Activity;  
4.import android.os.Bundle;  
5.import android.view.View;  
6. 
7.public class TestOnClickListener extends Activity {  
8.     
9.    @Override 
10.    public void onCreate(Bundle savedInstanceState) {  
11.        super.onCreate(savedInstanceState);  
12.        setContentView(R.layout.main);  
13.          
14. 
15.    }  
16.    public void myClickHandler01(View target){  
17.        setTitle("myClickHandler01");  
18.    }  
19.    public void myClickHandler02(View target){  
20.        setTitle("myClickHandler02");  
21.    }  
22.} 
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 myClickHandler01(View target){       setTitle("myClickHandler01");      }      public void myClickHandler02(View target){       setTitle("myClickHandler02");      }  }当然,你也可以采用这种写法:

将两个按钮设置到同一个Listener

android:onClick="myClickHandler"

android:onClick="myClickHandler"

Java代码
1.package com.ray.test;  
2. 
3.import android.app.Activity;  
4.import android.os.Bundle;  
5.import android.view.View;  
6. 
7.public class TestOnClickListener extends Activity {  
8.     
9.    @Override 
10.    public void onCreate(Bundle savedInstanceState) {  
11.        super.onCreate(savedInstanceState);  
12.        setContentView(R.layout.main);  
13.    }  
14.    public void myClickHandler(View target){  
15.         switch (target.getId()) {  
16.         case R.id.Button01:   
17.             setTitle("myClickHandler01");  
18.             break;  
19.         case R.id.Button02:   
20.             setTitle("myClickHandler02");  
21.             break;  
22.         }  
23.    }  
24.} 
分享到:
评论

相关推荐

    javasmack源码-Smack-Openfire:我使用Android,包括心跳和自动重新连接

    Android, include heartbeat and auto reconnect 图文详情地址: Smack是一个开源,易于使用的XMPP(jabber)客户端类库。优点:简单的,功能强大,给用户发送信息只需三行代码便可完成。缺点:API并非为大量并发...

    SpringBoot学习笔记完整教程

    Spring Boot 学习笔记,包括底层实现原理及代码实战,非常齐全,助你快速打通 Spring Boot 的各个环节。 1. Spring boot helloworld 2. Spring boot 返回 json 数据 ...16. Spring boot 集成 Fliter 和 Linstener

    SpringBoot 学习笔记+完整教程

    * 集成 Fliter 和 Linstener * 拦截器 HandlerInterceptor * 系统启动任务 CommandLineRunner * 集成 Junit 单元测试 * 读取系统环境变量 * 使用自定义 properties * 改变默认包扫᧿ * 自定义启动 Banner * 导入 ...

    Spring学习

    这里启动的Linstener均在应用之上就已经实例化完成了,而且Linstener也不是Spring组件,因此无法进行Spring的Bean注入操作。但是当上下文重新刷新完毕后可以通过context工厂获取Bean2. SpringBean生命周期3.Spring...

    组播练习.rar

    ---------如果我上面同时存在静态R3的静态rp的话,其实可以不用启用ip pim auto-rp linstener,因为只要224.0.1.39.40的原树建立起来就可以选择rp了,这个时候先用12.12.3.3的静态rp去建立源树,建好了选择好了rp,...

Global site tag (gtag.js) - Google Analytics