• 有关Botton的用法(一)


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

    点击button时 call doSomething();

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

     1     <Button
     2         android:layout_width="wrap_content"
     3         android:layout_height="wrap_content"
     4         android:text="Click here"
     5         android:onClick="doSth1"
     6         android:id="@+id/button" />
     7 
     8     <Button
     9         android:layout_width="wrap_content"
    10         android:layout_height="wrap_content"
    11         android:text="Button2"
    12         android:onClick="doSth2"//这两调的同一个函数doSth2()
    13         android:id="@+id/button2" />
    14 
    15     <Button
    16         android:layout_width="wrap_content"
    17         android:layout_height="wrap_content"
    18         android:text="Button3"
    19         android:onClick="doSth2"//这两调的同一个函数doSth2()
    20         android:id="@+id/button3" />
     1     public void doSth1(View view){
     2         Log.e("MainActivity", "Clicked1");
     3     }
     4 
     5     public void doSth2(View view){
     6 
     7         if(view.getId()==R.id.button2)
     8             Log.e("MainActivity","Clicked2");
     9         if(view.getId()==R.id.button3)
    10             Log.e("MainActivity","Clicked3");
    11     }

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

  • 相关阅读:
    oracle对象之序列
    PLSql工具介绍
    oracle对象之同义词
    oracle对象之视图
    缓存问题汇总
    消息队列问题汇总
    算法-排序算法-1
    redis-主从数据一致
    数据结构与算法-完全二叉树/满二叉树
    写缓冲器与无效化队列
  • 原文地址:https://www.cnblogs.com/turtle920/p/4860629.html
Copyright © 2020-2023  润新知