【Android的事件处理】-----基于监听的事件处理
来源:互联网 发布:手游商业版源码 编辑:程序博客网 时间:2024/05/14 01:47
一、知识点
1、定义:为用户动作提供响应的机制就是事件处理。
2、Android提供了两套强大的事件处理机制:
- 基于监听的事件处理
- 基于回调的事件处理
3、区别:一般来说,基于回调的事件处理可用于处理一些通用性的事件,基于回调的事件处理代码会比较简洁;但是对于某些特定的事件,无法使用基于回调的事件处 理,只能采用监听的事件处理。
4、事件监听有如下几个形式:
- 内部类形式
- 外部类形式
- Activity本身作为事件的监听器
- 匿名内部类
- 直接绑定标签
5、在基于监听的事件处理模式中,事件监听器必须实现事件监听接口,Android为不同的界面组件提供了不同的监听接口,这些接口通常以内部类的形式存。以View为例,它包含了如下几个内部接口:
- View.OnClickListener:单击事件的事件监听器必须实现的接口
- View.OnCreateContextMenuListener:创建上下文菜单事件的事件监听器必须实现的接口
- View.onFocusChangeListener:焦点改变事件的事件监听器必须实现的接口
- View.OnKeyListener:按键事件的事件监听器必须实现的接口
- View.OnLongClickListener:长按事件的事件监听器必须实现的接口
- View.OnTounchListener:触摸事件的事件监听器必须实现的接口
二、实战
例1:
1、效果图
2、demo目录
3、xml文件
<pre name="code" class="html"><?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" > <Button android:id="@+id/in" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="内部类监听按钮" android:textSize="21dp" /> <Button android:id="@+id/ni" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="匿名内部类监听按钮" android:textSize="21dp" /> <Button android:id="@+id/out" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="外部类监听按钮" android:textSize="21dp" /> <Button android:id="@+id/tip" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="clickTip" android:text="绑定标签监听按钮" android:textSize="21dp" /> <Button android:id="@+id/myself" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Activity本身监听按钮" android:textSize="21dp" /></LinearLayout>
4、外部类代码:
package com.example;import android.app.Activity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Toast;public class OutOnClickListener implements OnClickListener {private Activity MainActivity;public OutOnClickListener(Activity MainActivity) {this.MainActivity = MainActivity;}@Overridepublic void onClick(View v) {Toast.makeText(MainActivity, "外部类按钮", 2000).show();}}
5、主文件代码:
package com.example;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {Button in, out, ni, tip, myself;//定义按钮@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);initView();//// 初始化UINiSetOnClickListener();// 匿名内部类监听OutSetOnClickListener();// 外部类监听InSetOnClickListeer();// 内部类监听myself.setOnClickListener(this);// Activity本身作为事件的监听器}private void InSetOnClickListeer() {in.setOnClickListener(new InClickListener());}private void OutSetOnClickListener() {//直接调用外部类out.setOnClickListener(new OutOnClickListener(this));}private void NiSetOnClickListener() {ni.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "匿名内部类按钮", 2000).show();}});}public void clickTip(View v) {Toast.makeText(this, "绑定标签按钮", 2000).show();}private void initView() {in = (Button) findViewById(R.id.in);out = (Button) findViewById(R.id.out);ni = (Button) findViewById(R.id.ni);tip = (Button) findViewById(R.id.tip);myself = (Button) findViewById(R.id.myself);}class InClickListener implements OnClickListener {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "内部类按钮", 2000).show();}}@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "Activity本身按钮", 2000).show();}}
0 0
- 【Android的事件处理】-----基于监听的事件处理
- Android的事件处理之基于监听的事件处理
- Android的事件处理之基于监听的事件处理
- android事件处理机制-基于监听的事件处理
- Android事件处理之一 基于监听的事件处理
- Android中的事件处理总结-基于监听的事件处理
- Android事件处理之基于监听的事件处理
- Android事件处理之基于监听的事件处理
- Android中的事件处理总结-基于监听的事件处理
- Android中的事件处理总结-基于监听的事件处理
- Android基于监听的事件处理
- Android基于监听的事件处理机制
- Android基于监听的事件处理
- android基于监听的事件处理机制
- 基于监听的事件处理
- android 监听事件的处理
- 【Android】基于监听的事件处理和基于回调的事件处理
- Android基于回调的事件处理与基于监听的事件处理
- 如何使用wifi连接adb,不需要usb线
- MyEclipse 2015破解
- MongoDB usage
- linux下SVN服务器如何搭建和使用
- malloc、calloc、realloc的区别
- 【Android的事件处理】-----基于监听的事件处理
- 七分养护三分维修 关注车辆的生命周期保养
- CocoaPods更新慢的问题
- 我对人工智能的哲学思考
- git学习笔记
- KMP算法
- 查看oracle数据库的连接数以及用户
- meaven 下载Jar包
- 在基于tomcat的web project中加载dll文件的方法(来源于实际项目经验)