Android基础

View XML属性的onClick

2021-08-19  本文已影响0人  Tank_Mao

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
上一篇 下一篇

猜你喜欢

热点阅读