惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security Archives - TechRepublic
MongoDB | Blog
MongoDB | Blog
量子位
博客园 - 叶小钗
罗磊的独立博客
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
MyScale Blog
MyScale Blog
GbyAI
GbyAI
Help Net Security
Help Net Security
Y
Y Combinator Blog
Engineering at Meta
Engineering at Meta
Hacker News - Newest:
Hacker News - Newest: "LLM"
Latest news
Latest news
H
Hacker News: Front Page
Blog — PlanetScale
Blog — PlanetScale
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
S
Schneier on Security
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
Recorded Future
Recorded Future
S
Securelist
博客园 - Franky
Application and Cybersecurity Blog
Application and Cybersecurity Blog
A
About on SuperTechFans
N
News and Events Feed by Topic
AI
AI
T
Tenable Blog
N
News | PayPal Newsroom
C
Cybersecurity and Infrastructure Security Agency CISA
V
V2EX - 技术
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
L
LINUX DO - 热门话题
N
Netflix TechBlog - Medium
S
SegmentFault 最新的问题
T
The Blog of Author Tim Ferriss
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google Online Security Blog
Google Online Security Blog
S
Security Affairs
Webroot Blog
Webroot Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 三生石上(FineUI控件)
C
Comments on: Blog
G
GRAHAM CLULEY

博客园 - Rainbow

正向代理与反向代理(转) Java equals()和hashCode()的作用 java 内存模型 java 面向对象 java 集合 java string java 数据类型 Java 笔记 字符编码详解及由来(UNICODE,UTF-8,GBK)[转帖] C# 值类型和引用类型 说明 Android drawable文件夹含义 Android TextView setText 使用 转一篇博文 Java中==和equals的区别 FTP 链接上,但是列不出目录 出现:425 Can't open data connection Android开发中插入新的Activity CentOS常用命令 CentOS5.6 python2.4.3升级到2.7.2 yum命令全集详解
Android Button 单击事件
Rainbow · 2012-01-04 · via 博客园 - Rainbow

2012-01-04 18:29  Rainbow  阅读(6628)  评论()    收藏  举报

方法一:在XML文件中指定 单击事件函数

<Button
            android:id="@+id/button1"
            android:layout_width="120dip"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="59dp"
            android:onClick="onclicklistener"
            android:text="@string/mybuttonstr" />

然后在代码中实现这个函数。注意,函数需要public 要不会异常。

 public void onclicklistener(View tager)
    {
        Log.v("MyTag", "onClick");
    }

方法二:

在activity 的onCreate 中 找到button,然后给它赋上事件监听器。这个方式非常普遍。

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button but=(Button)this.findViewById(id.button1);
        
        
        but.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.v("MyTag", "onClick");
            }
        });
    }

方法三:

这个方法其实是方法二的变种。就是提前定义一个OnClickListener 的handler,然后可以将这个activity中所有的button多使用这个handler,通过判断不同id来进行不同的逻辑。这个方式适合比较多的button的情况使用。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        View.OnClickListener handler=new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switch (v.getId()){
                case id.button1:
                    Log.v("MyTag", "Button1 OnClick");
                    break;
                case id.button2:
                    Log.v("MyTag", "Button2 OnClick");
                    break;
                }
                
            }
            
        };
        
        Button btn1=(Button)this.findViewById(id.button1);
        Button btn2=(Button)this.findViewById(id.button2);
        
        btn1.setOnClickListener(handler);
        btn2.setOnClickListener(handler);

}