Android菜鸟的成长笔记(11)——Android中的事件处理
16lz
2022-01-16
Android提供了两种方式来处理事件,一个是基于回调的事件处理,另一个是基于监听的事件处理,举个例子:
基于回调的事件处理
@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubreturn super.onTouchEvent(event);}基于监听的事件处理
button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub}});一般来说,基于回调的事件处理可用于处理一些通用性的行为,而对于某些行为只能通过监听事件处理。
一、监听事件
二、回调事件监听事件中,主要涉及三类对象:
EventSource(事件源):事件发生的场所,例如按钮对象。
Event(事件):事件的相关信息的封装类
EventListener(事件监听器):负责监听事件源,并对事件做出响应。
Android中的事件处理机制是一种委派式的方式,普通组件(事件源)将整个事件处理委托给特定的对象(事件监听器),当该事件源发生指定的事件时,就通知所委托的事件监听器,由事件监听器来处理这个事件。
例如:
package com.example.testlistener;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnLongClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity {EditText txt;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button bn = (Button)findViewById(R.id.bn);txt = (EditText) findViewById(R.id.txt);bn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {txt.setText("我单击了Button");}});bn.setOnLongClickListener(new OnLongClickListener() {@Overridepublic boolean onLongClick(View arg0) {txt.setText("我长按了Button");return true;}});}}
如果监听机制是一种委托式的事件处理,那么回调机制则恰好相反,对于基于回调的事件处理模型来说,事件源与事件监听器是同一个对象,当用户在某个组件上激发事件时,组件自己特定的方法会处理该事件。 例如: 新建一个MyButton类继承自Button三、系统设置事件package com.example.testlistener;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.widget.Button;public class MyButton extends Button{public MyButton(Context context){super(context);}public MyButton(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}public MyButton(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stub}@Overridepublic boolean onTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_UP:this.setText("点击了我");break;default:break;}return super.onTouchEvent(event);}}在MyButton中重写了onTouchEvent方法,这就是回调监听函数。 activity_main.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" ><EditText android:id="@+id/txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false" android:textSize="12pt"/><com.example.testlistener.MyButton android:id="@+id/bn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点我啊"/></LinearLayout>运行结果:
Configuration类专门用于描述手机设备上的配置信息,这些配置信息即包括用户的特定配置也包括系统的动态设备配置。 例如:bn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {txt.setText("我单击了Button");Configuration cfg = getResources().getConfiguration();//判断屏幕方向String screen = cfg.orientation == Configuration.ORIENTATION_LANDSCAPE ? "横向屏幕":"竖向屏幕";//手机的MNC码String mncCode = cfg.mnc + "";String naviName = cfg.orientation == Configuration.NAVIGATION_NONAV ? "没有方向控制":cfg.orientation == Configuration.NAVIGATION_WHEEL ? "滚轮控制方向":cfg.orientation == Configuration.NAVIGATION_DPAD? "方向键控制方向":"轨迹球控制方向";String touchName = cfg.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH ? "无触摸屏":"支持触摸屏";System.out.println("screen = " + screen);System.out.println("mncCode = " + mncCode);System.out.println("naviName = " + naviName);System.out.println("touchName = " + touchName);}});运行结果:
更多相关文章
- Android中RecyclerView Adapter的骚操作
- android实现电话状态监控
- Android(安卓)EventBus使用,粘性事件postSticky使用
- android 获得焦点(View get focus)
- Android事件传递机制【Touch事件】
- Android—— 4.2 Vold挂载管理_CommandListener (二)
- Android(安卓)检测SD卡应用
- Android(安卓)DownloadManager下载完成事件监听(系列4)
- Android点击监听事件