Android中给按钮注册事件的四种方法
以给Button按钮注册点击事件为例,点击按钮时候会弹出123.
1.在xml中用Button的属性响应对应的方法
xml文件: main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main_ativity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="defind_click"
android:text="按钮"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Activity组件: Main_ativity.java
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view