包含Event Source 事件源,即各个控件
Event 事件,通常是用户的一次操作
Event 事件监听器,负责监听事件源所发生的事,并做出对应的回应
(1)通过findViewById(int resId)获取局部控件实例
(2)事件源注册事件监听
(3)类实现View.OnclickLisenter接口并重写onClick()方法
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="提示信息" android:textSize="24sp" /> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="接口实现事件处理模型" android:textSize="24sp" /> </LinearLayout>
public class MainActivity extends AppCompatActivity implements View.OnClickListener { TextView txt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_layout); txt = (TextView) findViewById(R.id.txt); Button button_one = (Button) findViewById(R.id.btn1); button_one.setOnClickListener(this); } @Override public void onClick(View view) { txt.setText("实现接口事件处理模型"); } }
public class MainActivity extends AppCompatActivity { TextView txt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_layout); txt = (TextView) findViewById(R.id.txt); //======内部类事件处理模型 Button button_two = (Button) findViewById(R.id.btn2); MyActivity myact = new MyActivity(); button_two.setOnClickListener(myact); } class MyActivity implements View.OnClickListener { @Override public void onClick(View view) { txt.setText("内部类事件处理模型"); } } }
public class MainActivity extends AppCompatActivity { TextView txt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_layout); txt = (TextView) findViewById(R.id.txt); Button button_three = (Button) findViewById(R.id.btn3); button_three.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { txt.setText("匿名内部类事件处理模型"); } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="提示信息" android:textSize="24sp" /> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="接口实现事件处理模型" android:textSize="24sp" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="内部类事件处理模型" android:textSize="24sp" /> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="匿名内部类事件处理模型" android:textSize="24sp" /> </LinearLayout>
package com.songjialongs.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { TextView txt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_layout); txt = (TextView) findViewById(R.id.txt); //======实现接口事件处理模型 Button button_one = (Button) findViewById(R.id.btn1); button_one.setOnClickListener(this); //======内部类事件处理模型 Button button_two = (Button) findViewById(R.id.btn2); MyActivity myact = new MyActivity(); button_two.setOnClickListener(myact); //======匿名内部类事件处理模型 Button button_three = (Button) findViewById(R.id.btn3); button_three.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { txt.setText("匿名内部类事件处理模型"); } }); } @Override public void onClick(View view) { txt.setText("实现接口事件处理模型"); } class MyActivity implements View.OnClickListener { @Override public void onClick(View view) { txt.setText("内部类事件处理模型"); } } }