zoukankan      html  css  js  c++  java
  • Android菜鸟的成长笔记(11)——Android中的事件处理

    Android提供了两种方式来处理事件,一个是基于回调的事件处理,另一个是基于监听的事件处理,举个例子:

    基于回调的事件处理

    	@Override
    	public boolean onTouchEvent(MotionEvent event) {
    		// TODO Auto-generated method stub
    		return super.onTouchEvent(event);
    	}
    基于监听的事件处理

    	button.setOnClickListener(new OnClickListener() {
    			
    		@Override
    		public 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;
    	@Override
    	protected 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() {
    			
    			@Override
    			public void onClick(View arg0) {
    				txt.setText("我单击了Button");
    			}
    		});
    		
    		bn.setOnLongClickListener(new OnLongClickListener() {
    			
    			@Override
    			public 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
    	}
    
    	@Override
    	public 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() {
    			
    			@Override
    			public 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);
    			}
    		});
    运行结果:









  • 相关阅读:
    数据抓去之初次接触
    PHP学习
    git预览项目
    正在加载中动画链接 css左右、关闭按钮
    正则表达式
    【独家】K8S漏洞报告|近期bug fix解读&1.11主要bug fix汇总
    关于k8s这项大动作,预示着边缘计算迎来“开源”发展的新周期……
    idou老师教你学istio :基于角色的访问控制
    K8s爆严重安全漏洞?有何应对措施与建议
    【容器魔方解读】AWS Re:Invent 2018大会
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6469206.html
Copyright © 2011-2022 走看看