View XML属性的onClick

android:onClick


Name of the method in this View's context to invoke when the view is clicked. 
This name must correspond to a public method that takes exactly one parameter of type View. 
For instance, if you specify android:onClick="sayHello",
 you must declare a public void sayHello(View v) method of your context (typically, your Activity).

May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;

举个例子:
xml里加一行代码

<Button
        android:id="@+id/cal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="cal"
        android:text="计算"/>

java代码里添加下面这个方法,注意必须要是public。

public void cal(View source){
        Toast.makeText(this, "您点击了“计算按钮!", Toast.LENGTH_LONG).show();
    }
效果图.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容