<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here"
android:onClick="doSomething"//用来设置onClick时MainActivity中调用的函数
android:id="@+id/button" />
     @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void doSomething(View view){
Log.e("MainActivity","Clicked");
}

点击button时 call doSomething();

如果有多个Button,则通过下面两种方式做出不同的响应:

     <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here"
android:onClick="doSth1"
android:id="@+id/button" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:onClick="doSth2"//这两调的同一个函数doSth2()
android:id="@+id/button2" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"
android:onClick="doSth2"//这两调的同一个函数doSth2()
android:id="@+id/button3" />
     public void doSth1(View view){
Log.e("MainActivity", "Clicked1");
} public void doSth2(View view){ if(view.getId()==R.id.button2)
Log.e("MainActivity","Clicked2");
if(view.getId()==R.id.button3)
Log.e("MainActivity","Clicked3");
}

如上,在doSth2()中通过view.getId()来获取不同的值,注意@+id/button3和R.id.button3是分别在xml和java中对同一个整数值的描述

有关Botton的用法(一)的更多相关文章

  1. 有关Botton的用法(二)

    关于设置listener监听onClicked事件的步骤分析 Steps: 1.tell android you are interested in listening to a button cli ...

  2. RegisterStartupScript和RegisterClientScriptBlock的用法

    RegisterStartupScript和RegisterClientScriptBlock的用法 RegisterStartupScript(key, script) RegisterClient ...

  3. CSS3 clip-path 用法介绍

    一.基本概念 刷新 QQ 空间动态时,发现一则广告,随着用户上下滑动动态列表,就会自动切换广告图片,这样的效果对移动端本就不大的屏幕来说,无疑是很精妙的考虑,这样的效果是怎么实现的呢? 你可以点击这里 ...

  4. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  5. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

  6. Java中的Socket的用法

                                   Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...

  7. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

  8. python enumerate 用法

    A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...

  9. [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结

    本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...

随机推荐

  1. Valid Number,判断是否为合法数字

    问题描述: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " ...

  2. http客户端-基于boost开发

    http客户端-基于boost开发 基于BOOST编写的http客户端,作为BOOST开发学习之用.目前支持功能: http协议,单向链接返回http response code 200 可conte ...

  3. Java多线程 - 线程组

    Java使用ThreadGroup来表示线程组,用以对一批线程进行分类管理. Java允许程序对线程组直接进行控制,对线程组的控制相当于同时控制这批线程: 用户创建的所有线程都属于指定线程组,如果程序 ...

  4. Tkinter Button按钮组件如何调用一个可以传入参数的函数

    这里我们要使用python的lambda函数,lambda是创建一个匿名函数,冒号前十传入参数,后面是一个处理传入参数的单行表达式. 调用lambda函数返回表达式的结果. 首先让我们创建一个函数fu ...

  5. 禁用Browser Link

    Browser Link是VS 2013开始引入的一个强大功能,让前端代码(比如AngularJS的代码)在VS中的修改更加轻而易举. 前端代码是运行在浏览器中,而Visual Studio通常只会和 ...

  6. jquery attr与prop的区别与联系

    最近开发中发现用attr无法设置checkbox的选中事件,在网上找了下说要用prop,所以总结下两者的区别. 1.操作的对象不同 attr:操作的是HTML文档节点属性 prop:操作的是js对象属 ...

  7. GAN的原理入门

    开发者自述:我是这样学习 GAN 的 from:https://www.leiphone.com/news/201707/1JEkcUZI1leAFq5L.html   Generative Adve ...

  8. L142

    keep half an eye on something分神留意splash out随意花钱 大肆挥霍half a mind有想做某事go Dutch v. 各自付帐,打平伙chance in a ...

  9. How to Have a Healthy Relationship --shanbei 为单身节写

    我在扇贝发现一片好文. Sometimes relationships can seem like a lot of work until you sit back and realize just ...

  10. H5 项目问题总结

    //一.HTML页面结构 <meta name="viewport" content="width=device-width,initial-scale=1.0,m ...