button
在xml中的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="OnclickButtonAction"
android:text="Button" />
</RelativeLayout>
1 onClick属性是点击时触发的事件:
public void OnclickButtonAction(View view){
System.out.println("123");
}
2 使用setOnClickListener添加监听器对象
Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// Do something in response to button click
}
});
Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// Do something in response to button click
}
});